Detailed explanation of Java unit test Mockito

  • 2021-10-25 06:32:52
  • OfStack

Introduction to Mockito

When a method of an mock object is called, the actual method is not executed, but the default value of the type is returned, such as null for object, 0 for int, etc. Otherwise, the return value of the method is specified by specifying when (method). thenReturn (value). At the same time, the mock object can be traced, and the verify method can be used to see if it has been called. The spy object executes the real method by default, and the return value can be overridden by when. thenReturn. It can be seen that mock only avoids executing some methods and directly returns the specified value, which is convenient for other tests.

Service Test Case

Dependence of need


  <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>2.23.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <version>2.1.13.RELEASE</version>
        </dependency>

Code Sample


@RunWith(MockitoJUnitRunner.class)
@SpringBootTest()
public class StudentServiceTest {
    @InjectMocks
    StudentService studentService = new StudentServiceImpl();
    @Mock
    StudentDAO     studentDAO;


    @Before
    public void before(){
        Mockito.doReturn(new StudentDO(" Zhang 3", 18)).when(studentDAO).read(Mockito.anyString());
    }

    @Test
    public void testRead(){
        StudentDO read = studentService.read("");
        Assert.assertNotNull(read);
    }
}

Controller Test Case

Dependence of need


<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.1.14.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <version>2.4.0</version>
        </dependency>

Code Sample


@RunWith(MockitoJUnitRunner.class)
@SpringBootTest()
public class StudentControllerTest {
    @Resource
    MockMvc mockMvc;

    @InjectMocks
    StudentController studentController;
    @Mock
    StudentService    studentService;

    @Before
    public void before() {
        mockMvc = MockMvcBuilders.standaloneSetup(studentController).build();
        Mockito.doReturn(new StudentDO(" Zhang 3", 18)).when(studentService).read(Mockito.anyString());
    }

    @Test
    public void testRead() throws Exception {
        MockHttpServletRequestBuilder request = MockMvcRequestBuilders.get("/student/read/1");
        mockMvc.perform(request)
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.name").value(" Zhang 3"));
    }

}

Related articles: