Error creating object MockHttpServletResponse for unit testing

后端 未结 3 2160
甜味超标
甜味超标 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

提交回复
热议问题