वसंत 3.1 से, हम JavaConfig का उपयोग आसानी से @Enable* एनोटेशन के लिए कर सकते हैं।क्यों वसंत 3.1 WebMvcConfig के साथ इकाई परीक्षण विफल रहता है?
इसलिए मैंने WebMvc कॉन्फ़िगरेशन सेट करने के लिए WebConfig बनाया, और इसका परीक्षण करने का प्रयास किया। लेकिन अगर मैं WebMvcConfigurerAdapter या WebMvc कॉन्फ़िगरेशन का विस्तार करता हूं WebConfig के साथ समर्थन यूनिट परीक्षण ServletContext की कमी के कारण विफल रहता है। कोड और संदेश नीचे की तरह दिखते हैं।
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() {}
}
संदेश
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
कैसे इकाई ठीक से परीक्षण करने के लिए WebConfig?
संपादित
रूप गार्सिया ने कहा, इस बग स्प्रिंग 3.2.0.RC1 में तय हो गई है।
बस परीक्षा कक्षा में @WebAppConfiguration एनोटेशन जोड़ें।
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes=WebConfig.class)
public class TestFail {
@Test
public void test() {}
}
मुझे समझ नहीं आता कि कैसे इस सवाल का तो कुछ वोट है। मुझे लगता है कि वहाँ वसंत 3.1 niceties के साथ एकीकरण परीक्षण करने की कोशिश कर रहे हैं ... –
सवाल ठीक वही था जो मैं खोज रहा था और उत्तर "संपादन" खंड –