Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoSuchMethodError: javaxservlet.http.HttpServletRequest.isAsyncStarted()Z

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)`
like image 627
Roger Avatar asked Sep 19 '14 18:09

Roger


2 Answers

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)

like image 50
Sam Brannen Avatar answered Oct 22 '22 22:10

Sam Brannen


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>
like image 2
Pol Vandekasseie Avatar answered Oct 22 '22 23:10

Pol Vandekasseie