Reputation: 6689
I am trying to create some tests for my controllers using spring test framework.
Following this article: http://blog.springsource.org/2012/11/12/spring-framework-3-2-rc1-spring-mvc-test-framework/ I created this test class
@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"));
}
}
and my testContext.xml is
<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>
now, when I run my tests I get a lot of messages like:
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>)].
so there is some problem with inferring types by mockito, but still when I enter the debug mode and inspect my controller I can see that objects were injected correctly. (I am using @Autowire
annotation in my controller).
Anyway, when I try to run this test I am getting test failure because of
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)
what could be the source of my problem? I think that I followed every step from this tutorial.
Upvotes: 1
Views: 4149
Reputation: 31197
The NoSuchMethodError
indicates that you likely have an incompatible version of the Servlet API on your test classpath.
Also, you'll need to include your Spring MVC configuration as well as the bean definition for your ServiceRequestController
within your test XML config, either directly, via an import, or by declaring a separate XML config file via @ContextConfiguration
.
Upvotes: 1