我正在尝试使用spring测试框架为我的控制器创建一些测试。
关注本文:http://blog.springsource.org/2012/11/12/spring-framework-3-2-rc1-spring-mvc-test-framework/我创建了此测试类
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("classpath:testContext.xml")
public class ServiceRequestControllerTest {
@Autowired
private WebApplicationContext wac;
@Autowired
private ServiceRequestController productController;
@Autowired
private WSServiceRequestIO wsServiceRequestIO;
@Autowired
private Open311ResponseMarshaller open311ResponseMarshaller;
@Autowired
private Open311GETQueryStringParser open311GETQueryStringParser;
@Autowired
private Open311POSTQueryStringParser open311POSTRequestParser;
@Autowired
private Open311ResponseFactory responseFactory;
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void testTest() throws Exception {
this.mockMvc.perform(get("/requests"));
}
}
我的testContext.xml是
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<bean class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="utils.webservice.WSServiceRequestIO" />
</bean>
<bean class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="open311.model.Open311ResponseMarshaller" />
</bean>
<bean class="org.mockito.Mockito" factory-method="mock">
<constructor-arg
value="open311.utils.Open311POSTQueryStringParser" />
</bean>
<bean class="org.mockito.Mockito" factory-method="mock">
<constructor-arg
value="open311.utils.Open311GETQueryStringParser" />
</bean>
<bean class="org.mockito.Mockito" factory-method="mock">
<constructor-arg
value="open311.model.Open311ResponseFactory" />
</bean>
<context:component-scan base-package="open311.controller" />
<mvc:annotation-driven />
<mvc:resources mapping="/services*" location="/resources/" />
</beans>
现在,当我运行测试时,我收到很多消息,如:
2013-04-19 14:33:34 INFO GenericTypeResolver:216 - Could not determine the target type for type argument [T] for method [public static <T> T org.mockito.Mockito.mock(java.lang.Class<T>)].
因此通过mockito推断类型存在一些问题,但是当我进入调试模式并检查我的控制器时,我仍然可以看到对象被正确注入。 (我在控制器中使用@Autowire
注释。)
无论如何,当我尝试运行此测试时,由于
而导致测试失败org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoSuchMethodError: javax/servlet/http/HttpServletResponse.getContentType()Ljava/lang/String;
at org.springframework.web.servlet.DispatcherServlet.triggerAfterCompletionWithError(DispatcherServlet.java:1259)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:920)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:816)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:740)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:801)
at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:66)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:168)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:136)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:134)
at open311.controller.ServiceRequestControllerTest.testTest(ServiceRequestControllerTest.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:48)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
at java.lang.reflect.Method.invoke(Method.java:600)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
可能是我的问题的根源?我认为我遵循了本教程的每一步。
答案 0 :(得分:1)
NoSuchMethodError
表示您的测试类路径上可能存在不兼容的Servlet API版本。
此外,您需要在测试XML配置中包含Spring MVC配置以及ServiceRequestController
的bean定义,可以直接,通过导入,也可以通过声明单独的XML配置文件@ContextConfiguration
。