6

I am trying to run a test with JUnit and Mockito against a spring REST webservice I am building. I came across a bug when trying to run the JUnit test and can't find any information on the problem. The stacktrace is listing the error line as the .andDo(print()) though I got that line directly from a spring.io tutorial http://spring.io/guides/tutorials/rest/3/

Test class code:

public class TestSuite {
    MockMvc mockMvc;

@Mock
RestController controller;

@Before
public void setup(){
    MockitoAnnotations.initMocks(this);
    this.mockMvc = standaloneSetup(controller)
        .setMessageConverters(new MappingJackson2HttpMessageConverter()).build();
}

@Test
public void testREST() throws Exception {
    when(controller.getThing(any(Integer.class))).thenReturn(TestFixture.getREST(1));
    this.mockMvc.perform(get("/{number}", String.valueOf(number))
    .accept(MediaType.APPLICATION_JSON))
    .andDo(print())
    .andExpect(status().isNotFound());
}}

Stacktrace:

java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.isAsyncStarted()Z
at org.springframework.test.web.servlet.result.PrintingResultHandler.printAsyncResult(PrintingResultHandler.java:131)
at org.springframework.test.web.servlet.result.PrintingResultHandler.handle(PrintingResultHandler.java:80)
at org.springframework.test.web.servlet.MockMvc$1.andDo(MockMvc.java:155)
at org.company.test.TestSuite.testREST(TestSuite.java:53)`
Xstian
  • 8,184
  • 10
  • 42
  • 72
Roger
  • 10,851
  • 3
  • 26
  • 39
  • Possible [solution](http://stackoverflow.com/questions/23759690/java-lang-nosuchmethoderror-javax-servlet-http-httpservletrequest-isasyncstarte) – Grice Sep 19 '14 at 19:29

2 Answers2

7

You are using an older version of the Servlet spec (e.g., 2.5); whereas, the version of spring-test that you are using requires at least Servlet 3.0.

As mentioned in the Testing Improvements section of the reference manual,

As of Spring 4.0, the set of mocks in the org.springframework.mock.web package is now based on the Servlet 3.0 API.

So, although Spring Framework 4.0 and 4.1 support Servlet 2.5 for deployment, the Servlet API mocks and the Spring MVC Test Framework in the spring-test module require Servlet 3.0 (specifically version 3.0.1 or higher).

Regards,

Sam (lead of the spring-test module)

Sam Brannen
  • 29,611
  • 5
  • 104
  • 136
  • I have an app facing the same scenario. The container I need to deploy the app to is only Servlet 2.5 capable. Is there a way to get spring-test to work with 2.5 at least for unit testing? – user6123723 Sep 16 '15 at 18:54
  • No, it is not possible to use the Mock Servlet API in `spring-test` with Servlet 2.5. However, as long as you do not rely on any Servlet 3.x features in your production code (e.g., controllers), there is no problem with testing against Servlet 3.x while deploying against Servlet 2.5. – Sam Brannen Oct 01 '15 at 16:01
2

I was getting this error because the dependency to servlet hadn't been added to my pom.xml adding the next few lines solved my issue

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>test</scope>
</dependency>