Od wiosny 3.1 możemy łatwiej używać JavaConfig dzięki adnotacjom @Enable*.Dlaczego testowanie urządzenia przy użyciu Spring 3.1 WebMvcConfig nie działa?
Zrobiłem WebConfig, aby ustawić konfigurację WebMvc i próbowałem ją przetestować. Ale jeśli rozszerzam WebMvcConfigurerAdapter lub WebMvcConfigurationSupport z WebConfig, test jednostki kończy się niepowodzeniem z powodu braku ServletContext. Kod i wiadomości wyglądają jak poniżej.
WebConfig.java
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurationSupport {}
Test.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=WebConfig.class)
public class TestFail {
@Test
public void test() {}
}
Wiadomość
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
Jak przetestować urządzenie z webconfig prawidłowo?
Edit
Jak powiedział Garcia, ten problem został rozwiązany w Spring 3.2.0.RC1.
Po prostu dodaj adnotację @WebAppConfiguration do klasy testowej.
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes=WebConfig.class)
public class TestFail {
@Test
public void test() {}
}
ja nie rozumiem, jak ta kwestia ma tak mało głosów. Przypuszczam, że są ludzie, którzy próbują przeprowadzić testy integracyjne z wiosenną 3.1 niceties ... –
Pytanie było dokładnie tym, czego szukałem, a odpowiedź jest zawarta w sekcji "Edycja" –