简体   繁体   English

为什么IntelliJ能够看到我的应用程序上下文,但是当我运行我的测试时他们看不到它?

[英]Why would IntelliJ be able to see my application context, but when I run my tests they can't see it?

I had some JUnit tests which were running fine. 我有一些运行正常的JUnit测试。 Recently they have stopped working, without any change being done to the code itself. 最近他们已停止工作,对代码本身没有任何改变。 I'm assuming that some change must have happened to my configuration settings somewhere, but I cannot figure out where the problem lies. 我假设某些地方的配置设置发生了一些变化,但我无法弄清问题所在。

I have a test that looks like the one below: 我有一个如下所示的测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:ApplicationContext.xml")
public class MyTests {

    @Autowired
    MyTestClass myClass

    @Test
    public void myFirstTest() {
        // Test code
    }

}

The directory structure looks like this 目录结构如下所示

  • MyPackage MyPackage的
    • test 测试
      • java java的
        • MyTests MyTests
    • resources 资源
      • ApplicationContext.xml applicationContext.xml中

The most confusing thing about all of this is that IntelliJ understands where the context is. 关于所有这一切最令人困惑的是IntelliJ了解上下文的位置。 It links to it in my code, I can see the annotation of the leaf and I can click it to bring up the correct application context, and next to the bean autowired annotation I can see the little bean icon and when I click it it takes me to the definition inside the application context. 它在我的代码中链接到它,我可以看到叶子的注释,我可以单击它以显示正确的应用程序上下文,并且在bean自动装配注释旁边我可以看到小豆图标,当我点击它时需要它我在应用程序上下文中的定义。 So I know that it's there and can be read. 所以我知道它在那里,可以阅读。 So why can JUnit not see it? 那么为什么JUnit看不到呢? What configuration have I managed to lose/change? 我设法丢失/更改了哪些配置?

NOTE: I'm trying both debug/run test through IntelliJ. 注意:我正在尝试通过IntelliJ进行调试/运行测试。


EDIT: My specific error is: 编辑:我的具体错误是:

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:122)
    at org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:105)
    at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:74)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [dataApplicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [dataApplicationContext.xml] cannot be opened because it does not exist
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadBeanDefinitions(AbstractGenericContextLoader.java:233)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:117)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:248)
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64)
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91)
    ... 28 more
Caused by: java.io.FileNotFoundException: class path resource [dataApplicationContext.xml] cannot be opened because it does not exist
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:328)
    ... 40 more

Just try this: 试试这个:

@ContextConfiguration(locations="classpath:ApplicationContext.xml")

change to: 改成:

@ContextConfiguration(locations={"classpath:**/ApplicationContext.xml"})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Jenkins上发布时,为什么我的测试无法看到我的系统属性? - Why can't my tests see my system property when I release on Jenkins? 尝试从Servlet连接时,Tomcat也看不到我的CONTEXT.XML。我也使用IDEA Intellij - Tomcat doesn't see my CONTEXT.XML when trying to connect from Servlet also i use IDEA Intellij 为什么我在jsp中看不到我的$ {token}? - Why can't i see my ${token} in my jsp? 我看不到应用程序数据库的最新版本 - I can't see the latest version of my application's database 从Wireshark中可以看到我的Java应用程序中的udp组播流量 - I can't see the udp multicast traffic in my java application while I can see from the wireshark 为什么在“位置”标签中看不到任何更改 - Why can't I see any changes in my “Place” tab 为什么我看不到我的项目在 netbeans 中运行? - Why can't I see my project running in netbeans? Java-为什么我在JFrame中看不到我的列表? - Java - Why i can't see my list in JFrame? 我不明白为什么我的java扫描程序抛出“NoSuchElementException” - I can't see why my scanner in java is throwing a “NoSuchElementException” 为什么我在avd应用程序菜单中看不到我的应用程序? - Why can't I see my app in the avd app menu?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM