others - java @Mock, @MockBean 与 Mockito.mock( ) 之间的差异

在创建测试和模拟依赖项时,这三种方法之间的区别是什么?

@MockBean:


@MockBean


MyService myservice;



@Mock:


@Mock


MyService myservice;



Mockito.mock()


MyService myservice = Mockito.mock(MyService.class);



时间:

纯Mockito库


import org.mockito.Mock;


...


@Mock


MyService myservice;




import org.mockito.Mockito;


...


MyService myservice = Mockito.mock(MyService.class);



来自Mockito库,在功能上是等价的,
它们允许模拟类或接口,并记录和验证行为。




@Before 


public void initMocks() {


 MockitoAnnotations.initMocks(this);


}



启用Mockito注释的另一种方法是使用@RunWith来注释测试类,方法是指定执行此任务的MockitoJUnitRunner以及其他有用的东西:


@RunWith(org.mockito.runners.MockitoJUnitRunner.class)


public MyClassTest{...}



Spring Boot库包装Mockito库

这是一个Spring Boot类:


import org.springframework.boot.test.mock.mockito.MockBean;


...


@MockBean


MyService myservice;



类包含在spring-boot-test库中。

它允许在spring ApplicationContext中添加Mockito mocks,





Spring Boot文档总结得非常好:

通常,@WebMvcTest将仅限于一个控制器,并且与@MockBean一起使用,为所需的协作者提供模拟实现。

以下是一个示例:


import org.junit.Test;


import org.junit.runner.RunWith;


import org.mockito.Mockito;


import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;


import org.springframework.boot.test.mock.mockito.MockBean;


import org.springframework.http.MediaType;


import org.springframework.test.context.junit4.SpringRunner;


import org.springframework.test.web.servlet.MockMvc;



import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;


import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;



@RunWith(SpringRunner.class)


@WebMvcTest(FooController.class)


public class FooControllerTest {



 @Autowired


 private MockMvc mvc;



 @MockBean


 private FooService fooServiceMock;



 @Test


 public void testExample() throws Exception {


 Foo mockedFoo = new Foo("one","two");



 Mockito.when(fooServiceMock.get(1))


 .thenReturn(mockedFoo);



 mvc.perform(get("foos/1")


 .accept(MediaType.TEXT_PLAIN))


 .andExpect(status().isOk())


 .andExpect(content().string("one two"));


 }



}



@Mock: (org.mockito.Mock )


Mark a field as a mock. 


 - Allows shorthand mock creation. 


 - Minimizes repetitive mock creation code. 


 - Makes the test class more readable. 


 - Makes the verification error easier to read because the field name is used to identify the mock.



@MockBean: (org.springframework.boot.test.mock.mockito.MockBean)


Annotation that can be used to add mocks to a Spring ApplicationContext. Can be used as a class level annotation or on fields in either @Configuration classes, or test classes that are @RunWith the SpringRunner. 



Mocks can be registered by type or by bean name. 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. 



When @MockBean is used on a field, as well as being registered in the application context, the mock will also be injected into the field.



Mockito.mock()


Its just the representation of a @Mock. 



...