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

