无法在使用SpringJUnit4ClassRunner的测试中加载Spring应用程序上下文

时间:2016-06-29 09:23:49

标签: spring-mvc junit spring-test

我无法弄清楚为什么Spring无法在如下定义的测试类中加载应用程序上下文:

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:228)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:230)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:249)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
    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:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
    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:62)
    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 [springMVC-config/mainController-servlet.xml]; nested exception is java.io.FileNotFoundException: class path resource [springMVC-config/mainController-servlet.xml] cannot be opened because it does not exist

文件'mainController-servlet.xml'位于“myproject \ src \ main \ webapp \ WEB-INF \ springMVC-config \ mainController-servlet.xml”

我得到的错误是:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/springMVC-config/mainController-servlet.xml")
public class DiagnosticsControllerTest {
...
}

我尝试更改上下文配置的位置,如下所示:

Date.parse

4 个答案:

答案 0 :(得分:1)

这样可行。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:**/webapp/WEB-INF/springMVC-config/mainController-servlet.xml")
public class DiagnosticsControllerTest {
...
}

答案 1 :(得分:1)

除了无法引用XML配置文件的正确路径之外,事实是:

您甚至没有在您提供的示例中使用 Spring TestContext Framework

相反,您只使用Mockito和Spring MVC测试(即MockMvc)。

因此,您只需完全删除@RunWith@ContextConfiguration声明即可。

答案 2 :(得分:0)

webapp不在类路径上,因此无法找到该文件。

请参阅Spring @ContextConfiguration how to put the right location for the xml,了解将Spring配置放入src/main/resources的原因不明确。

解决方案是将您的配置移到web.xml,然后调整,然后重新加载Spring配置文件以查看它们的类路径。如果您使用的是classpath:文件,则应该像为repositories { mavenLocal() mavenCentral() jcenter() maven { url "https://<myrepo>/repo" } } 添加前缀一样简单。

还有其他解决方案,但我认为这是长期的最佳选择。

答案 3 :(得分:0)

为了让我摆脱错误“无法加载applicationContext”,我不得不将Spring升级到4.3.6.RELEASE和JUnit到4.12。在我引入lambda之后尝试使用Java 1.8运行JUnit测试时发生了这个错误,即使该文件位于src/main/resources