为什么使用Spring 3.1 WebMvcConfig进行单元测试失败?

San*_*Lee 22 java spring spring-3

从Spring 3.1开始,由于@ Enable* annotations ,我们可以更轻松地使用JavaConfig .

所以我创建了一个WebConfig来设置WebMvc配置,并尝试对其进行测试.但是,如果我使用WebConfig扩展WebMvcConfigurerAdapter或WebMvcConfigurationSupport,则由于缺少ServletContext,单元测试失败.代码和消息如下所示.

WebConfig.java

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurationSupport {}
Run Code Online (Sandbox Code Playgroud)

Test.java

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=WebConfig.class)
public class TestFail {
    @Test
    public void test() {}
}
Run Code Online (Sandbox Code Playgroud)

信息

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
...
Caused by: java.lang.IllegalArgumentException: A ServletContext is required to configure default servlet handling
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer.<init>(DefaultServletHandlerConfigurer.java:54)
    at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.defaultServletHandlerMapping(WebMvcConfigurationSupport.java:253)
    at com.zum.news.comments.web.WebConfig$$EnhancerByCGLIB$$8bbfcca1.CGLIB$defaultServletHandlerMapping$10(<generated>)
    at com.zum.news.comments.web.WebConfig$$EnhancerByCGLIB$$8bbfcca1$$FastClassByCGLIB$$19b86ad0.invoke(<generated>)
    at net.sf.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:215)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:280)
    at com.zum.news.comments.web.WebConfig$$EnhancerByCGLIB$$8bbfcca1.defaultServletHandlerMapping(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:149)
    ... 41 more
Run Code Online (Sandbox Code Playgroud)

如何正确地测试WebConfig?

编辑

正如加西亚所说,这个错误在Spring 3.2.0.RC1中得到修复.

只需在测试类中添加@WebAppConfiguration注释即可.

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes=WebConfig.class)
public class TestFail {
    @Test
    public void test() {}
}
Run Code Online (Sandbox Code Playgroud)

Ste*_*eve 12

正如Guido之前提到的,这已经从3.2解决了.以下是如何利用新测试改进的详细信息.要确保为测试加载servlet上下文,您需要使用@WebAppConfiguration并定义AnnotationConfigWebContextLoader为上下文加载器来注释测试,如下所示:

@RunWith(SpringJUnit4ClassRunner.class)    
@WebAppConfiguration
@ContextConfiguration(
    classes = MyWebConfig.class, 
    loader = AnnotationConfigWebContextLoader.class)
public class MyTest {
    //...
}
Run Code Online (Sandbox Code Playgroud)


Sla*_*hin 7

如果@EnableWebMvc注释需要,ServletContext那么我建议将配置拆分为bean定义,这些定义将用于单元测试和应用程序和框架使用的其他配置.在这种情况下,应用程序将导入两个配置,单元测试将只导入一个.

BeansConfig.java:

@Configuration
public class BeansConfig {
    @Bean
    MyBean myBean() {
        return new MyBean()
    }
}
Run Code Online (Sandbox Code Playgroud)

WebConfig.java:

@Configuration
@EnableWebMvc
@Import(BeansConfig.class)
public class WebConfig extends WebMvcConfigurationSupport {}
Run Code Online (Sandbox Code Playgroud)

TestFail.java:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=BeansConfig.class)
public class TestFail {
    @Test
    public void test() {}
}
Run Code Online (Sandbox Code Playgroud)


Gui*_*ido 6

Spring 3.1中存在一个错误,您可以在以下两个问题中找到答案:

如果您找到Spring 3.1的解决方法,请告诉我们,如果没有,我们必须等到3.2.我不得不说我刚刚尝试使用Spring 3.2.0.M2并且它仍然不适合我.