Unable to mock Service class in Spring MVC Controller tests

后端 未结 7 786
渐次进展
渐次进展 2020-12-02 06:44

I have a Spring 3.2 MVC application and am using the Spring MVC test framework to test GET and POST requests on the actions of my controllers. I am using Mockito to mock the

7条回答
  •  眼角桃花
    2020-12-02 07:18

    If on SpringBoot use Springs own @MockBean. - as the docks say:

    Any existing single bean of the same type defined in the context will be replaced by the mock. If no existing bean is defined a new one will be added.

     @RunWith(SpringRunner.class)
     public class ExampleTests {
    
         @MockBean
         private ExampleService service;
    

提交回复
热议问题