12

I am trying to get my feet wet with TDD. I am trying to write unit test cases for controllers using Mockito in conjunction with MockMvc and Junit.

But I am getting a runtime error thereby failing my test. At first I was facing problem in initializing the MockMvc instance in the setup due to failure in finding the javax.servlet.SessionCookieConfig.

This I resolved by downloading the javax.servlet api and configuring it into the build path of the project but then I am facing the

java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.isAsyncStarted()

while using perform() on the MockMvc instance.

Can anyone tell me what to do with this kind of dependencies as I think it is occurring due to the incompatible server servlet-api and javax.servlet api.

EDIT : I am posting the code I am using using for unit testing but I don't think it would be any help but just in case :

@RunWith(MockitoJUnitRunner.class)
public class MyControllerTest {

    @InjectMocks
    private MyController myController = new MyController();

    @Mock
    private MyService myService = new MyServiceImpl();

    private MockMvc mockMvc;

    @Before
    public void setUp(){
        this.mockMvc = MockMvcBuilders.standaloneSetup(myController).build();
    }

    @Test
    public void testList() throws Exception{
        A a = new A();
        a = createMockClassA();

        Mockito.when(myService.getServiceForA(Mockito.anyMapOf(String.class, String.class))).thenReturn(a);

        MvcResult result = this.mockMvc.perform(get("/somePath/")).param("someExpectedParam","value").andReturn(); 

        System.out.println(result.getResponse().getContentAsString());

    }



    private static A createMockClassA(){
        A a = new A();
        a.setId(i);
        a.setTitle("mock-" + i);
        return a;
    }
}
Sourabh
  • 1,253
  • 6
  • 21
  • 39

6 Answers6

11

This sounds very much like you have the wrong version of the servlet API in the class path.

Check when isAsyncStarted was added to the API and make sure the one you reference in your classpath is at least that version or higher.

In order to find the location where the 'wrong' class version is comming from you can use the

-verbose:class

Argument for java. It will list all the classes loaded and if I remember correctly whery they get loaded from. See http://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html for details.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • Hi Jens, I checked out the API isAsyncStarted has been added in servlet 3.0 and I am using java.servlet-3.0.jar in my project build. – Sourabh May 20 '14 at 12:39
  • Thats ok for development. Check if the server is using the same servlet api level. – Manish Mudgal May 20 '14 at 12:41
  • Server is of same servlet api level. – Sourabh May 21 '14 at 06:07
  • @Sourabh added some info on how to find the class causing problems – Jens Schauder May 21 '14 at 06:24
  • Hey Jens, I could find HttpServletRequest. It gives: Exception in thread "main" java.lang.NoClassDefFoundError: HttpServletRequest Caused by: java.lang.ClassNotFoundException: HttpServletRequest at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) – Sourabh May 21 '14 at 06:40
  • I think by default there is javax.servlet included in some package because even when i removed the package 3.0 from the build path I can still import javax.servlet. Do you know which package it is in so that I could update the whole package BTW I am using jdk 1.6 in case it helps – Sourabh May 21 '14 at 06:41
  • the package will be the same. Question is, which jar is it. That's what -verbose:class will show you. – Jens Schauder May 21 '14 at 06:43
2

This happens when your development and production environments are using different servlet API versions.

While building with tomcat7(for example) it supports servlet 3 and hence you will not receive any error.

While doing the same on a lower version tomcat, it will throw error.

Solution:

Either upgrade one of the environments to support servlet 3 or just downgrade your code to use servlet 2.5

Manish Mudgal
  • 1,166
  • 1
  • 9
  • 24
  • Hi Manish, I thought of the same but shockingly I have been using apache tomcat 7.0.40 and javax.servlet-3.0.jar. I can't downgrade javax servlet to 2.5 since it doesn't contains of SessionCookieConfig class. So upgrading tomcat would have been an option but its already at the compatible version. – Sourabh May 20 '14 at 12:43
1

Error message indicates that you have wrong version of Servlet API in your classpath.

If you are using Gradle the execute

gradle dependencies

analyze the dependency tree and exclude the 'servlet-api' dependencies with version less than 3.0. You can do the following to exclude

compile ('javax.servlet:jsp-api:2.0'){
    exclude module : 'servlet-api'
}

There can be multiple dependencies which further include servlet-api-2.x. Exclude all those

Robin
  • 6,879
  • 7
  • 37
  • 35
  • Thanks, this was the solution in my case. org.apache:storm-core uses servlet-api 2.5 but io.confluent:kafka-schema-registry uses servlet-api 3.1 . I had to exclude the dependency from storm to make my code work. – asmaier Nov 11 '16 at 09:16
0

I found the solution for my error. The actual servlet api that was getting provided was of gwt-servlet.jar and the servlet-api in the gwt-servlet.jar was of the older version. Thus I had configure my build path to make the project point to the latest servlet-api while building.

As for the correct answer, I think my vote goes to Jens since he did gave the solution closest as per the scenario.

Thanks everyone. :)

Sourabh
  • 1,253
  • 6
  • 21
  • 39
0

I solve this problem by adding to java build path tomcat as a server runtime library

mockito javax.servlet.SessionCookieConfig problem

e2rabi
  • 4,728
  • 9
  • 42
  • 69
0

I had a similar issue few of my Junit tests were not working (in intellij idea) and I was also getting java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.... for those unit tests. When I try to compile the complete project using gradle from the directory through command prompt by using the command gradle clean build then the code was being compiled successfully. Whereas problem was with the Junit tests in Intellij idea they were showing the above mentioned error. I simply changed my gradle version from 3.4.1 to 2.1.3. I do not know why now my Junit tests are compiling as well as my code is being compiled through intellij as well as through command prompt. The same problem occured with another colleague of mine and he too changed the gradle version from 4 to some version of 2. The problem was resolved.

Waqar Detho
  • 1,502
  • 18
  • 17