java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRe

时间:2022-12-27
本文介绍了java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.isAsyncStarted() 将 Mockito 与 Junit 一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 TDD.我正在尝试使用 Mockito 结合 MockMvc 和 Junit 为控制器编写单元测试用例.

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.

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

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.

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

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()

在 MockMvc 实例上使用 perform() 时.

while using perform() on the MockMvc instance.

谁能告诉我如何处理这种依赖关系,因为我认为这是由于服务器 servlet-api 和 javax.servlet api 不兼容而发生的.

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;
    }
}

推荐答案

这听起来很像您在类路径中的 servlet API 版本错误.

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

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

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

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

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.

这篇关于java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.isAsyncStarted() 将 Mockito 与 Junit 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:如何在 Java (jUnit4) 中轻松模拟出静态方法 下一篇:Mockito 与 JMockit 之间的比较 - 为什么 Mockito 的投票

相关文章

最新文章