简体   繁体   English

java.lang.NoSuchMethodError:javaxservlet.http.HttpServletRequest.isAsyncStarted()Z

[英]java.lang.NoSuchMethodError: javaxservlet.http.HttpServletRequest.isAsyncStarted()Z

I am trying to run a test with JUnit and Mockito against a spring REST webservice I am building. 我正在尝试使用JUnit和Mockito对我正在构建的Spring REST Web服务进行测试。 I came across a bug when trying to run the JUnit test and can't find any information on the problem. 我在尝试运行JUnit测试时遇到了一个错误,但无法找到有关该问题的任何信息。 The stacktrace is listing the error line as the .andDo(print()) though I got that line directly from a spring.io tutorial http://spring.io/guides/tutorials/rest/3/ stacktrace将错误行列为.andDo(print())虽然我直接从spring.io教程获得该行http://spring.io/guides/tutorials/rest/3/

Test class code: 测试类代码:

public class TestSuite {
    MockMvc mockMvc;

@Mock
RestController controller;

@Before
public void setup(){
    MockitoAnnotations.initMocks(this);
    this.mockMvc = standaloneSetup(controller)
        .setMessageConverters(new MappingJackson2HttpMessageConverter()).build();
}

@Test
public void testREST() throws Exception {
    when(controller.getThing(any(Integer.class))).thenReturn(TestFixture.getREST(1));
    this.mockMvc.perform(get("/{number}", String.valueOf(number))
    .accept(MediaType.APPLICATION_JSON))
    .andDo(print())
    .andExpect(status().isNotFound());
}}

Stacktrace: 堆栈跟踪:

java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.isAsyncStarted()Z
at org.springframework.test.web.servlet.result.PrintingResultHandler.printAsyncResult(PrintingResultHandler.java:131)
at org.springframework.test.web.servlet.result.PrintingResultHandler.handle(PrintingResultHandler.java:80)
at org.springframework.test.web.servlet.MockMvc$1.andDo(MockMvc.java:155)
at org.company.test.TestSuite.testREST(TestSuite.java:53)`

You are using an older version of the Servlet spec (eg, 2.5); 您正在使用较旧版本的Servlet规范(例如,2.5); whereas, the version of spring-test that you are using requires at least Servlet 3.0. 然而,您使用的spring-test版本至少需要Servlet 3.0。

As mentioned in the Testing Improvements section of the reference manual, 如参考手册的“ 测试改进”部分所述,

As of Spring 4.0, the set of mocks in the org.springframework.mock.web package is now based on the Servlet 3.0 API. 从Spring 4.0开始, org.springframework.mock.web包中的org.springframework.mock.web集现在基于Servlet 3.0 API。

So, although Spring Framework 4.0 and 4.1 support Servlet 2.5 for deployment , the Servlet API mocks and the Spring MVC Test Framework in the spring-test module require Servlet 3.0 (specifically version 3.0.1 or higher). 因此,虽然Spring Framework 4.0和4.1支持Servlet 2.5进行部署 ,但是Spring spring-test模块中的Servlet API模拟和Spring MVC Test Framework需要Servlet 3.0(特别是3.0.1或更高版本)。

Regards, 问候,

Sam (lead of the spring-test module) Sam( spring-test模块的领导者)

I was getting this error because the dependency to servlet hadn't been added to my pom.xml adding the next few lines solved my issue 我收到此错误是因为servlet的依赖关系尚未添加到我的pom.xml中,添加下几行解决了我的问题

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>test</scope>
</dependency>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.NoSuchMethodError:javax.servlet.http.HttpServletRequest.isAsyncStarted - java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.isAsyncStarted java.lang.NoSuchMethodError:使用Mockito和Junit时的javax.servlet.http.HttpServletRequest.isAsyncStarted() - java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.isAsyncStarted() while using Mockito with Junit java.lang.NoSuchMethodError:javax.servlet.http.HttpServletRequest.startAsync - java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.startAsync java.lang.NoSuchMethodError:javax.servlet.http.HttpServletRequest.getPart - java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getPart java.lang.NoSuchMethodError:javax.servlet.http.HttpServletRequest.getParts() - java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getParts() java.lang.NoSuchMethodError: &#39;javax.servlet.http.HttpServletMapping javax.servlet.http.HttpServletRequest.getHttpServletMapping()&#39; - java.lang.NoSuchMethodError: 'javax.servlet.http.HttpServletMapping javax.servlet.http.HttpServletRequest.getHttpServletMapping()' Spring 启动:java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getHttpServletMapping()Ljavax/servlet/http/HttpServletMapping; - Spring boot: java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getHttpServletMapping()Ljavax/servlet/http/HttpServletMapping; java.lang.NoSuchMethodError:javax.servlet.http.HttpServletRequest.getPart(Ljava / lang / String;)Ljavax / servlet / http / Part; - java.lang.NoSuchMethodError: javax.servlet.http.HttpServletRequest.getPart(Ljava/lang/String;)Ljavax/servlet/http/Part; java.lang.NoSuchMethodError:javax.servlet.http.Cookie.setHttpOnly(Z)V - java.lang.NoSuchMethodError: javax.servlet.http.Cookie.setHttpOnly(Z)V 面对 java.lang.NoSuchMethodError: 'javax.servlet.http.HttpServletMapping javax.servlet.Z80791B3AE7002CB88C246876D9FAAletServMappingZ。 - Facing java.lang.NoSuchMethodError: 'javax.servlet.http.HttpServletMapping javax.servlet.http.HttpServletRequest.getHttpServletMapping()' error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM