SpringBoot結(jié)合mockito測試實戰(zhàn)
題目起的很像那么回事,但是這篇博客本身寫的很簡單。
我不想寫說一堆概念,然后闡釋各種概念是什么意思。我喜歡的是直接從例子出發(fā)。
package com.example.demo.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.entity.Person;
import com.example.demo.mapper.PersonMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Random;
@RequestMapping({"/user"})
@RestController
@Slf4j
public class UserController {
@Autowired
PersonMapper personMapper;
@RequestMapping("/getUserById")
public Person getUserById(Long id){
Person person =personMapper.selectById(id);
if ("張三".equals(person.getName())){
return person;
}
if ("李四".equals(person.getName())){
person.setAge(17);
return person;
}
return person;
}
}
//具體的PersonMapper如下
package com.example.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.Person;
import org.springframework.stereotype.Component;
@Component(value = "personMapper")
public interface PersonMapper extends BaseMapper<Person> {
}首先就是上面的例子,getUserById。
這個方法里面的邏輯可以分為兩部分,一個是通過mapper從db里面拿數(shù)據(jù),還有就是很多額外完全沒有邏輯的代碼,不用管它的具體語義,反正就是寫些業(yè)務(wù)邏輯。
我現(xiàn)在想測試的目的就是先假定mapper本身是OK的,然后驗證那些無意義的額外邏輯也是正確的。
ok,看測試代碼。
package com.example.demo.controller;
import com.example.demo.entity.Person;
import com.example.demo.mapper.PersonMapper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.boot.test.context.SpringBootTest;
import static org.mockito.Mockito.when;
@SpringBootTest
@ExtendWith(MockitoExtension.class)
public class MockitoTest {
@InjectMocks
private UserController userController;
@Mock
private PersonMapper personMapper;
@Test
public void testGet(){
Person person = new Person();
person.setName("張三");
Long id = 15L;
when(personMapper.selectById(id)).thenReturn(person);
Person result = userController.getUserById(id);
Assertions.assertEquals(person,result);
}
}運行之后:

那pom文件呢?
我用的是
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>沒有別的依賴了。
ok,測試代碼已經(jīng)能跑了,現(xiàn)在就來解釋代碼。
代碼解釋
@InjectMocks
這個注解就是說我要測試被這個注解標注的類,他里面每個方法都會真正的被執(zhí)行。
換句話說,被這個注解標注的類,是我們真正想測試的類。
InjectMocks 被這個注解標注的類,是我們真正想測試的類。
InjectMocks 被這個注解標注的類,是我們真正想測試的類。
InjectMocks 被這個注解標注的類,是我們真正想測試的類。
@Mock
被這個注解標注的是我們想忽略的或者說,默認就認為是ok的類。
when
personMapper本身就被@Mock標注了,那么其實里面的每個方法都不會真正的被執(zhí)行,在UserController的Person person =personMapper.selectById(id);里面selectById就直接返回一個null。
但是加上下面的代碼
when(personMapper.selectById(id)).thenReturn(person);
功能我就不解釋了。
到此這篇關(guān)于SpringBoot結(jié)合mockito測試實戰(zhàn)的文章就介紹到這了,更多相關(guān)SpringBoot mockito內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用SpringBoot+EasyExcel+Vue實現(xiàn)excel表格的導(dǎo)入和導(dǎo)出詳解
這篇文章主要介紹了使用SpringBoot+VUE+EasyExcel?整合導(dǎo)入導(dǎo)出數(shù)據(jù)的過程詳解,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08
Java 實戰(zhàn)項目錘煉之醫(yī)院門診收費管理系統(tǒng)的實現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+html+jdbc+mysql實現(xiàn)一個醫(yī)院門診收費管理系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11
SpringBoot連接MYSQL數(shù)據(jù)庫并使用JPA進行操作
今天給大家介紹一下如何SpringBoot中連接Mysql數(shù)據(jù)庫,并使用JPA進行數(shù)據(jù)庫的相關(guān)操作。2017-04-04
解決Android Studio安裝后運行出錯dose not...和Internal error...
這篇文章主要介紹了解決Android Studio安裝后運行出錯dose not...和Internal error...的相關(guān)資料,需要的朋友可以參考下2017-03-03
Java關(guān)于遠程調(diào)試程序教程(以Eclipse為例)
這篇文章主要介紹了Java關(guān)于遠程調(diào)試程序教程(以Eclipse為例),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06

