问题
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;
}
}
回答1:
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.
回答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
回答3:
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
回答4:
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. :)
回答5:
I solve this problem by adding to java build path tomcat as a server runtime library
回答6:
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.
来源:https://stackoverflow.com/questions/23759690/java-lang-nosuchmethoderror-javax-servlet-http-httpservletrequest-isasyncstarte