java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.isAsyncStarted() 同时使用 Mockito 和 Junit

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23759690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:04:00  来源:igfitidea点击:

java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.isAsyncStarted() while using Mockito with Junit

javajunitmockingmockitomockmvc

提问by Sourabh

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.

我正试图用 TDD 弄湿我的脚。我正在尝试使用 Mockito 以及 MockMvc 和 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.

但是我收到了一个运行时错误,因此我的测试失败了。起初我在设置中初始化 MockMvc 实例时遇到问题,因为找不到javax.servlet.SessionCookieConfig.

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

我通过下载javax.servletapi 并将其配置到项目的构建路径中解决了这个问题,但随后我面临

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

while using perform()on the MockMvc instance.

perform()MockMvc 实例上使用时。

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.

谁能告诉我如何处理这种依赖关系,因为我认为这是由于服务器 servlet-api 和 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;
    }
}

采纳答案by Jens Schauder

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

这听起来很像您在类路径中使用了错误版本的 servlet API。

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

检查何时isAsyncStarted添加到 API 并确保您在类路径中引用的那个版本至少是该版本或更高版本。

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.htmlfor details.

java的参数。它将列出所有加载的类,如果我没记错的话,它们是从哪里加载的。有关详细信息,请参阅http://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html

回答by Manish Mudgal

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

当您的开发和生产环境使用不同的 servlet API 版本时会发生这种情况。

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

在使用 tomcat7(例如)构建时,它支持 servlet 3,因此您不会收到任何错误。

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

在较低版本的tomcat上做同样的事情,它会抛出错误。

Solution:

解决方案

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

升级其中一种环境以支持 servlet 3 或仅将代码降级以使用 servlet 2.5

回答by Sourabh

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.

我找到了解决我的错误的方法。提供的实际 servlet api 是 gwt-servlet.jar,gwt-servlet.jar 中的 servlet-api 是旧版本。因此,我配置了我的构建路径,使项目在构建时指向最新的 servlet-api。

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

至于正确答案,我认为我的票投给了 Jens,因为他确实根据场景给出了最接近的解决方案。

Thanks everyone. :)

谢谢大家。:)

回答by Robin

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

错误消息表明您的类路径中的 Servlet API 版本错误。

If you are using Gradle the execute

如果您使用 Gradle 执行

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

分析依赖树,排除版本低于 3.0 的 'servlet-api' 依赖。您可以执行以下操作来排除

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

可以有多个依赖项,进一步包括 servlet-api-2.x。排除所有这些

回答by e2rabi

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

我通过添加到 java 构建路径 tomcat 作为服务器运行时库来解决这个问题

mockito javax.servlet.SessionCookieConfig problem

mockito javax.servlet.SessionCookieConfig 问题

回答by Waqar Detho

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 buildthen 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.

我有一个类似的问题,我的一些 Junit 测试无法正常工作(在 Intellij 想法中),而且我也在java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest....进行这些单元测试。当我尝试使用该命令通过命令提示符从目录中使用 gradle 编译完整项目时gradle clean build,代码已成功编译。而问题在于 Intellij 想法中的 Junit 测试,他们显示了上述错误。我只是将我的 gradle 版本从 3.4.1 更改为 2.1.3。我不知道为什么现在我的 Junit 测试正在编译以及我的代码正在通过 intellij 和命令提示符编译。我的另一位同事也出现了同样的问题,他也将 gradle 版本从 4 更改为 2 的某个版本。问题解决了。