Java單元測試Mockito的使用詳解
Mockito簡介
調(diào)用mock對象的方法時,不會執(zhí)行真實(shí)的方法,而是返回類型的默認(rèn)值,如object返回null, int返回0等,否則通過指定when(方法).thenReturn(value)來指定方法的返回值。同時mock對象可以進(jìn)行跟蹤,使用verify方法看是否已經(jīng)被調(diào)用過。而spy對象,默認(rèn)會執(zhí)行真實(shí)方法,返回值可以通過when.thenReturn進(jìn)行覆蓋。可見mock只要避開了執(zhí)行一些方法,直接返回指定的值,方便做其他測試。
Service測試用例
需要的依賴
<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>
代碼示例
@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測試用例
需要的依賴
<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>
代碼示例
@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("張三")); } }
到此這篇關(guān)于單元測試-Mockito的使用的文章就介紹到這了,更多相關(guān)單元測試 Mockito使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解SpringBoot中的統(tǒng)一結(jié)果返回與統(tǒng)一異常處理
這篇文章主要將通過詳細(xì)的討論和實(shí)例演示來幫助你更好地理解和應(yīng)用Spring Boot中的統(tǒng)一結(jié)果返回和統(tǒng)一異常處理,感興趣的小伙伴可以了解下2024-03-03java本機(jī)內(nèi)存分配Native?memory?allocation?mmap失敗問題解決
這篇文章主要介紹了java本機(jī)內(nèi)存分配Native?memory?allocation?mmap失敗問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Java使用枚舉替代if/else和switch-case語句的實(shí)踐
在軟件開發(fā)中if-else和switch-case語句經(jīng)常被用來處理不同的條件分支,但在大型項(xiàng)目中,這種做法可能導(dǎo)致代碼可讀性差、維護(hù)困難,這篇文章主要給大家介紹了關(guān)于Java使用枚舉替代if/else和switch-case語句的相關(guān)資料,需要的朋友可以參考下2024-09-09springboot啟動的注意事項(xiàng)之不同包下有同樣名字的class類問題
這篇文章主要介紹了springboot啟動的注意事項(xiàng)之不同包下有同樣名字的class類問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06java隊(duì)列中Queue與Deque的區(qū)別面試精講
這篇文章主要為大家介紹了java隊(duì)列中Queue與Deque的區(qū)別面試精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10Java數(shù)據(jù)結(jié)構(gòu)及算法實(shí)例:漢諾塔問題 Hanoi
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)及算法實(shí)例:漢諾塔問題 Hanoi,本文直接給出實(shí)現(xiàn)代碼,代碼中包含大量注釋,需要的朋友可以參考下2015-06-06