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

Spring mvc 无法在使用SpringJUnit4ClassRunner的测试中加载Spring应用程序上下文,spring-mvc,junit,spring-test,Spring Mvc,Junit,Spring Test,我不明白为什么Spring不能在定义如下的测试类中加载应用程序上下文: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"/springMVC-config/mainController-servlet.xml"}) public class DiagnosticsControllerTest { @Mock DiagnosticsService diagn

我不明白为什么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);
    }
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/springMVC-config/mainController-servlet.xml")
public class DiagnosticsControllerTest {
...
}
文件“maincontrollerservlet.xml”位于“myproject\src\main\webapp\WEB-INF\springMVC config\maincontrollerservlet.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(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);
    }
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("file:src/main/webapp/WEB-INF/springMVC-config/mainController-servlet.xml")
public class DiagnosticsControllerTest {
...
}

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

有关将Spring配置置于
webapp
中的原因的说明,请参阅

解决方案是将配置移动到
src/main/resources
,并调整加载Spring配置文件的方式,以便在类路径上查找它们。如果您使用的是
web.xml
文件,那应该很简单,只要在它们前面加上
classpath:

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

这会奏效的

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

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

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

相反,您只使用Mockito和springmvc测试(即,
MockMvc


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

为了消除错误“加载applicationContext失败”,我必须将Spring升级到4.3.6.RELEASE,将JUnit升级到4.12。在引入lambdas之后,当我尝试使用Java 1.8运行JUnit测试时,出现了这个错误,即使该文件位于
src/main/resources

中,要说服某人将这些配置文件移动到另一个位置将很困难:@Mihir Gohel,您的解决方案不起作用。我认为问题的根源在于我有几个XML文件(都在WEB-INF子文件夹中),而且我觉得并不是所有的都加载了。在调查Loooong堆栈跟踪之后,我发现了与某些bean的加载错误有关的其他错误:由以下原因引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为“XXX_ds”的bean“,其中'XXX_ds'是数据源的名称。\@Mihir Gohel:定义
@ContextConfiguration
时,IntelliJ中至少少了一个错误:我可以声明注释的自动连线控制器如下:
专用诊断控制器DiagnosticsController
带有
@autowired
注释。不,还有Spring无法绑定的其他bean以及数据源未找到问题。所以说ApplicationContext not found的初始错误仍然存在。真的很奇怪:(。你能试试我在代码中所做的编辑吗。不确定,但希望它能解决问题。或者你可以共享github repo链接,我会看看。