Error creating object MockHttpServletResponse for unit testing

后端 未结 3 2161
甜味超标
甜味超标 2021-01-12 17:07
  1. I was trying to write unit test for Servlet using sprint-test using mock object

  2. my maven dependency is:

    
       
    
            
相关标签:
3条回答
  • 2021-01-12 17:37

    According to the Spring Framework Reference for Testing you should be using annotations to autowire your mocks. The example in the spring reference:

    `

        @WebAppConfiguration
        @ContextConfiguration
        public class WacTests {
    
        @Autowired WebApplicationContext wac; // cached
    
        @Autowired MockServletContext servletContext; // cached
    
        @Autowired MockHttpSession session;
    
        @Autowired MockHttpServletRequest request;
    
        @Autowired MockHttpServletResponse response;
    
        @Autowired ServletWebRequest webRequest;
    
        //...
    }
    

    `

    A different example (without annotations) can be found here

    0 讨论(0)
  • 2021-01-12 17:49

    I had a similar issue and solved it by adding this dependency to my pom and there is no need to change your javaee-api to javax.servlet

    <dependency>
     <groupId>org.mortbay.jetty</groupId>
     <artifactId>servlet-api-2.5</artifactId>
     <version>6.1.11</version>
    </dependency>
    
    0 讨论(0)
  • 2021-01-12 17:55

    I need to replace dependency for javaee-api with javax.selvlet-api as below:

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
    </dependency>
    
    0 讨论(0)
提交回复
热议问题