Home >Java >javaTutorial >How to use Mockito for Java unit testing

How to use Mockito for Java unit testing

王林
王林forward
2023-04-19 23:22:091754browse

Mockito Introduction

When calling the method of the mock object, the real method will not be executed, but the default value of the return type, such as object returns null, int returns 0, etc., otherwise by specifying when (Method).thenReturn(value) to specify the return value of the method. At the same time, the mock object can be tracked and the verify method can be used to see whether it has been called. The spy object will execute the real method by default, and the return value can be overridden through when.thenReturn. It can be seen that as long as mock avoids executing some methods and directly returns the specified value, it is convenient for other tests.

Service test case

Required dependencies

  <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("张三", 18)).when(studentDAO).read(Mockito.anyString());
    }

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

Controller test case

Required dependencies

<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 Example

@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("张三", 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("张三"));
    }

}

The above is the detailed content of How to use Mockito for Java unit testing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete