【问题标题】:Fails to load Spring application context in tests with SpringJUnit4ClassRunner使用 SpringJUnit4ClassRunner 无法在测试中加载 Spring 应用程序上下文
【发布时间】:2016-10-31 21:59:58
【问题描述】:

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

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations={"/springMVC-config/mainController-servlet.xml"})
    public class DiagnosticsControllerTest {

    @Mock
    DiagnosticsService diagnosticsService;

    private DiagnosticsController diagnosticsController;

    private MockMvc mockMvc;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        diagnosticsController = new DiagnosticsController();
        diagnosticsController.setService(diagnosticsService);
        this.mockMvc = MockMvcBuilders.standaloneSetup(diagnosticsController).build();
    }

    @Test
    public void shouldRun() {
        assertTrue(1 == 1);
    }
}

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

我得到的错误是:

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

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

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

【问题讨论】:

    标签: spring-mvc junit spring-test


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

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

      因此,您可以完全删除 @RunWith@ContextConfiguration 声明。

      【讨论】:

        【解决方案3】:

        这会起作用。

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

        【讨论】:

        • 说服某人将这些配置文件移动到另一个位置将很困难:@Mihir Gohel,您的解决方案不起作用。我认为问题出在我有几个 XML 文件(都在 WEB-INF 子文件夹中)广告我的印象是并非所有文件都已加载。在调查了 looooong 堆栈跟踪之后,我发现了有关某些 bean 的加载错误的其他错误:原因:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为 'XXX_ds' 的 bean”,其中 'XXX_ds' 是一个名称数据源。
        • \@Mihir Gohel:在定义@ContextConfiguration 时,IntelliJ 中至少少了一个错误:我可以如下声明带注释的自动装配控制器:private DiagnosticsController diagnosticsController@Autowired 注释。
        • 不,还有其他 Spring 无法绑定的 bean 以及未找到数据源的问题。所以最初的错误说 ApplicationContext not found 仍然存在。真的很奇怪:(。
        • 你可以试试我在代码中所做的编辑。不确定,但希望它能解决问题。或者你可以分享github repo链接,我去看看。
        【解决方案4】:

        src/main/webapp 不在类路径中,因此找不到文件。

        请参阅 Spring @ContextConfiguration how to put the right location for the xml,了解为什么将 Spring 配置放入 webapp 是不好的。

        解决方案是将您的配置移动到src/main/resources,然后调整您正在加载的 Spring 配置文件以查看它们的类路径。如果您使用的是web.xml 文件,那应该就像在它们前面加上classpath: 一样简单。

        还有其他解决方案,但我觉得从长远来看这是最好的选择。

        【讨论】:

          猜你喜欢
          • 2013-09-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-02-03
          • 2021-06-23
          • 2018-07-11
          • 2014-09-03
          相关资源
          最近更新 更多