本文介绍了@ Mock,@ MovieBean和Mockito.mock()之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When creating tests and mocking dependencies, what is the difference between these three approaches?


  1. @MockBean:

  1. @MockBean:

@MockBean
MyService myservice;


  • @Mock:

  • @Mock:

    @Mock
    MyService myservice;
    


  • Mockito.mock()

  • 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库,功能相同。

    它们允许模拟类或接口并记录和验证行为。

    come from the Mockito library and are functionally equivalent.
    They allow to mock a class or an interface and to record and verify behaviors on it.

    使用注释的方式更短,如此优先,通常是首选。

    The way using annotation is shorter, so preferable and often preferred.

    请注意,要在测试执行期间启用Mockito注释,
    MockitoAnnotations.initMocks(this)必须调用静态方法。

    为了避免测试之间的副作用,建议在每次测试执行之前执行此操作:

    Note that to enable Mockito annotations during test executions, theMockitoAnnotations.initMocks(this) static method has to be called.
    To avoid side effect between tests, it is advised to do it before each test execution :

    @Before
    public void initMocks() {
        MockitoAnnotations.initMocks(this);
    }
    

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

    Another way to enable Mockito annotations is annotating the test class with @RunWith by specifying the MockitoJUnitRunner that does this task and also other useful things :

    @RunWith(org.mockito.runners.MockitoJUnitRunner.class)
    public MyClassTest{...}
    






    Spring Boot库包装Mockito库

    这确实是:

    import org.springframework.boot.test.mock.mockito.MockBean;
    ...
    @MockBean
    MyService myservice;
    

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

    The class is included in the spring-boot-test library.

    它允许在Spring ApplicationContext 中添加Mockito模拟。


    如果在上下文中存在与声明的类兼容的bean,则通过mock替换

    如果不是这样,那么在上下文中将mock添加作为bean。

    It allows to add Mockito mocks in a Spring ApplicationContext.
    If a bean, compatible with the declared class exists in the context, it replaces it by the mock.
    If it is not the case, it adds the mock in the context as a bean.

    Javadoc引用:

    Javadoc reference :

    ...

    如果在上下文
    中定义的任何相同类型的现有单个bean将被mock替换,如果没有定义现有bean,则将添加新的

    If 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.






    使用classic / plain Mockito时和使用 @MockBean 来自Spring Boot?


    When use classic/plain Mockito and when use @MockBean from Spring Boot ?

    单元测试旨在测试交流与其他组件隔离的单元和单元测试也有一个要求:在执行时间方面尽可能快,因为这些测试可能每天在开发人员机器上执行十几次。

    Unit tests are designed to test a component in isolation from other components and unit tests have also a requirement : being as fast as possible in terms of execution time as these tests may be executed each day dozen times on the developer machines.

    因此,这是一个简单的准则:

    Consequently, here is a simple guideline :

    当您编写一个不需要Spring Boot容器的任何依赖项的测试时,经典/普通Mockito是遵循的方式:它很快并且有利于隔离测试组件。

    如果你的测试需要依赖Spring Boot容器你还想添加或模拟其中一个容器bean:从Spring Boot中获取 @MockBean

    As you write a test that doesn't need any dependencies from the Spring Boot container, the classic/plain Mockito is the way to follow : it is fast and favors the isolation of the tested component.
    If your test needs to rely on the Spring Boot container and you want also to add or mock one of the container beans : @MockBean from Spring Boot is the way.

    Spring Boot的典型用法 @MockBean

    Typical usage of Spring Boot @MockBean

    当我们编写一个用 @WebMvcTest (web测试切片)注释的测试类时。

    As we write a test class annotated with @WebMvcTest (web test slice).

    总结了这一点:

    这是一个例子:

    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,@ MovieBean和Mockito.mock()之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    07-29 21:04
    查看更多