Home > Java > Java Tutorial > body text

How to use Mockito for Java unit testing

王林
Release: 2023-04-19 23:22:09
forward
1609 people have browsed it

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

  
            junit
            junit
            4.12
            test
        
        
            org.mockito
            mockito-core
            2.23.4
            test
        
        
            org.springframework.boot
            spring-boot-test
            2.1.13.RELEASE
        
Copy after login

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);
    }
}
Copy after login

Controller test case

Required dependencies


            org.springframework
            spring-test
            5.1.14.RELEASE
        
        
            com.jayway.jsonpath
            json-path
            2.4.0
        
Copy after login

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

}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!