当前位置:网站首页>Summary of mockito

Summary of mockito

2021-05-04 15:23:04 Kwanwooo

Why use Mock To test

  1. Traditional testing , Back end engineers rely on some third-party interfaces ( payment 、 Timing task is not convenient for real test interface ), Front end engineers rely on back-end interfaces , Test engineers rely on front-end interfaces
  2. Use Mock, Testing with virtual objects , Achieve decoupling 、 Concurrent development .

common mock frame

  • moco: Front end or test utilization moco Generate interface document
  • Mockito: The back-end using mockito Unit test

Mockito Common use of API

  1. add to Mockito rely on and Junit rely on
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>3.8.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
  1. Generate mock( simulation ) object ( A variety of ways )

Unless otherwise specified , This package is statically imported everywhere

import static org.mockito.Mockito.*;

Create directly mock object

//create mock
List mockedList = mock(List.class);

Inject through annotations mock object


@RunWith(MockitoJUnitRunner.class)
public class Test {

    @Mock
    private AService aService;

    @Test
    public void test() {
        //handle aService
    }
    interface AService {
        public boolean test();
    }

    @Service
    class AServiceImpl implements  AService {
        @Override
        public boolean test() {
            return false;
        }
    }

}

expand , establish spy object

        List mockList= mock(List.class);
        List spy = spy(mockList);
  1. Use mock object

Test pile (Stub) Pattern : By default , One mock Object returns as null Or the default value of the underlying type . The test pile function will override the function return value .

A simple example

        //  establish  mock  object 
        List mockedList = mock(List.class);

        //  Test pile 
        when(mockedList.get(0)).thenReturn("first");
        //  Continuous test pile 
        when(mockedList.get(1)).thenReturn("once").thenReturn("twice");
        //  Abnormal test pile 
        when(mockedList.get(2)).thenThrow(new RuntimeException());

        //  Print  first
        System.out.println(mockedList.get(0));
        //  Print  once
        System.out.println(mockedList.get(1));
        //  Print  twice
        System.out.println(mockedList.get(1));
        //  Print  null
        System.out.println(mockedList.get(999));
        //  newspaper  RuntimeException  abnormal 
        System.out.println(mockedList.get(2));

Use anyXXX()

        //  establish  mock  object 
        List mockedList = mock(List.class);

        when(mockedList.get(anyInt())).thenReturn("element");
        // Print  element
        System.out.println(mockedList.get(999));
        verify(mockedList).get(anyInt());

Number verification

        //  establish  mock  object 
        List mockedList = mock(List.class);

        mockedList.add("once");
        mockedList.add("twice");
        mockedList.add("twice");
        mockedList.add("three times");
        mockedList.add("three times");
        mockedList.add("three times");

        // 1 Time  once
        verify(mockedList, times(1)).add("once");
        // 2 Time  twice
        verify(mockedList, times(2)).add("twice");
        // 3 Time  three times
        verify(mockedList, times(3)).add("three times");
        //  Not once  never happened
        verify(mockedList, never()).add("never happened");

        //  expand 
        //  At least once  three times
        verify(mockedList, atLeastOnce()).add("three times");
        //  At least twice  five times
//        verify(mockedList, atLeast(2)).add("five times");
        //  most 5 Time  three times
//        verify(mockedList, atMost(5)).add("three times");

Sequential verification

        //  establish  mock  object ,  Performing sequential validation of individual objects 
        List singleMock = mock(List.class);

        singleMock.add("was added first");
        singleMock.add("was added second");

        InOrder inOrder = inOrder(singleMock);

        inOrder.verify(singleMock).add("was added first");
        inOrder.verify(singleMock).add("was added second");

        //  Create multiple  mock  object , Do more than one mock Object sequence validation 
        List firstList = mock(List.class);
        List secondList = mock(List.class);

        firstList.add("was added first");
        secondList.add("was added second");

        InOrder order = inOrder(firstList, secondList);
        order.verify(firstList).add("was added first");
        order.verify(secondList).add("was added second");

Reference documents

版权声明
本文为[Kwanwooo]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/05/20210504152238849M.html

随机推荐