java.lang.NoSuchMethodError:使用Mockito和Junit时的javax.servlet.http.HttpServletRequest.isAsyncStarted() [英] java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.isAsyncStarted() while using Mockito with Junit

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

问题描述

我试图用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.

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

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:使用Mockito和Junit时的javax.servlet.http.HttpServletRequest.isAsyncStarted()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆