Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Java unit tests how to use Mockito

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "how to use Java unit test Mockito". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Introduction to Mockito

When the method of the mock object is called, the real method is not executed, but the default value of the type is returned, such as object returns null, int returns 0, 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 tracked, using the verify method to see if it has been called. The spy object, by default, executes the real method, and the return value can be overridden by when.thenReturn. It can be seen that as long as mock avoids the implementation of some methods and directly returns the specified value, it is convenient to do other tests.

Service test case

Needed dependency

Junit junit 4.12 test org.mockito mockito-core 2.23.4 test org.springframework.boot spring-boot-test 2.1.13.RELEASE

Code example

@ RunWith (MockitoJUnitRunner.class) @ SpringBootTest () public class StudentServiceTest {@ InjectMocks StudentService studentService = new StudentServiceImpl (); @ Mock StudentDAO studentDAO; @ Before public void before () {Mockito.doReturn (new StudentDO ("Zhang San", 18)) .when (studentDAO) .read (Mockito.anyString ());} @ Test public void testRead () {StudentDO read = studentService.read ("); Assert.assertNotNull (read);}} Controller test case

Needed dependency

Org.springframework spring-test 5.1.14.RELEASE com.jayway.jsonpath json-path 2.4.0

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 ("Zhang San", 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 San")) This is the end of the content of "Java Unit Test Mockito". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report