1. 新建測試類
在IDEA中寫好的類中,右建GO TO,選擇TEST,如果沒有對應(yīng)的單元測試類就選擇創(chuàng)建涯雅。
在創(chuàng)建該類的單元測試類前,先講一下這幾個基本注解:
@Test:使用該注解標(biāo)注的public void方法會表示為一個測試方法展运;
@BeforeClass:表示在類中的任意public static void方法執(zhí)行之前執(zhí)行活逆;
@AfterClass:表示在類中的任意public static void方法之后執(zhí)行;
@Before:表示在任意使用@Test注解標(biāo)注的public void方法執(zhí)行之前執(zhí)行拗胜;
@After:表示在任意使用@Test注解標(biāo)注的public void方法執(zhí)行之后執(zhí)行蔗候;
2. 添加依賴
新建的springBoot項目中默認包含了spring-boot-starter-test的依賴,如果沒有包含可自行在pom.xml中添加依賴埂软。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
3. Mock的概念
所謂的mock就是創(chuàng)建一個類的虛假的對象锈遥,在測試環(huán)境中,用來替換掉真實的對象勘畔,以達到兩大目的:
- 驗證這個對象的某些方法的調(diào)用情況所灸,調(diào)用了多少次,參數(shù)是什么等等
- 指定這個對象的某些方法的行為炫七,返回特定的值爬立,或者是執(zhí)行特定的動作
使用Mock之前,需要在@Before或@BeforeClass對應(yīng)的方法中添加如下万哪,表示添加mock注解初始化侠驯。
MockitoAnnotations.initMocks(this);
另外需要補充以下幾個常用的測試注解:
@InjectMocks:通過創(chuàng)建一個實例,它可以調(diào)用真實代碼的方法奕巍,其余用@Mock(或@Spy)注解創(chuàng)建的mock將被注入到用該實例中吟策。
@Mock:對函數(shù)的調(diào)用均執(zhí)行mock(即虛假函數(shù)),不執(zhí)行真正部分的止。
@Spy:對函數(shù)的調(diào)用均執(zhí)行真正部分檩坚。
Mockito中的Mock和Spy都可用于攔截那些尚未實現(xiàn)或不期望被真實調(diào)用的對象和方法,并為其設(shè)置自定義行為诅福。二者的區(qū)別在于Mock不真實調(diào)用匾委,Spy會真實調(diào)用。
- @mock與@spy使用示例
(1)被測試類
class ExampleService {
int add(int a, int b) {
return a+b;
}
}
(2)測試類
import org.junit.Assert;
import org.junit.Test;
import static org.mockito.Mockito.*;
public class MockitoDemo {
@Spy
private ExampleService spyExampleService;
@Mock
private ExampleService mockExampleService;
// 測試 spy
@Test
public void test_spy() {
// 默認會走真實方法
Assert.assertEquals(3, spyExampleService.add(1, 2));
// 打樁后权谁,不會走了
when(spyExampleService.add(1, 2)).thenReturn(10);
Assert.assertEquals(10, spyExampleService.add(1, 2));
// 但是參數(shù)不匹配的調(diào)用剩檀,依然走真實方法
Assert.assertEquals(3, spyExampleService.add(2, 1));
}
// 測試 mock
@Test
public void test_mock() {
// 默認返回結(jié)果是返回類型int的默認值
Assert.assertEquals(0, mockExampleService.add(1, 2));
}
}
- @InjectMock和@Mock使用示例
(1)被測試類
class MockExampleService {
int add(int a, int b) {
return a+b;
}
}
class InjectMockExampleService {
@Autowired
private MockExampleService mockExampleService;
int add(int a, int b) {
return mockExampleService.add(a, b);
}
}
(2)測試類
import org.junit.Assert;
import org.junit.Test;
import static org.mockito.Mockito.*;
public class MockitoDemo {
@InjectMock
private InjectMockExampleService injectMockExampleService;
@Mock
private MockExampleService mockExampleService;
// 初始化漢順
@Before
public void init() throw Exception {
MockitoAnnotations.initMocks(this);
}
// 使用Mockito模擬
@Test
public void test() {
// 模擬MockExampleServiceadd函數(shù)對于任何參數(shù)返回都為10
when(mockExampleService.add(anyInt(), anyInt())).thenReturn(10);
// InjectMock會走真實的add方法,只不過mock會返回一個模擬的結(jié)果
Assert.assertEquals(10, injectMockExampleService.add(1, 2));
}
}
4. 常用的 Mockito 方法
Mockito的使用旺芽,一般有以下幾種組合:參考鏈接
- do/when:包括doThrow(…).when(…)/doReturn(…).when(…)/doAnswer(…).when(…)
- given/will:包括given(…).willReturn(…)/given(…).willAnswer(…)
- when/then: 包括when(…).thenReturn(…)/when(…).thenAnswer(…)/when(…).thenThrow(…)
Mockito 有多種匹配函數(shù),部分如下:
函數(shù)名 | 匹配類型 |
---|---|
any() | 所有對象類型 |
anyInt() | 基本類型 int、非 null 的 Integer 類型 |
anyChar() | 基本類型 char采章、非 null 的 Character 類型 |
anyShort() | 基本類型 short运嗜、非 null 的 Short 類型 |
anyBoolean() | 基本類型 boolean、非 null 的 Boolean 類型 |
anyDouble() | 基本類型 double悯舟、非 null 的 Double 類型 |
anyFloat() | 基本類型 float担租、非 null 的 Float 類型 |
anyLong() | 基本類型 long、非 null 的 Long 類型 |
anyByte() | 基本類型 byte抵怎、非 null 的 Byte 類型 |
anyString() | String 類型(不能是 null) |
anyList() | List<T> 類型(不能是 null) |
anyMap() | Map<K, V>類型(不能是 null) |
5. 參考
- https://blog.csdn.net/yangshengwei230612/article/details/104753603/
- https://blog.csdn.net/jieyingxiao/article/details/98217955
- https://blog.csdn.net/u011047968/article/details/91970070?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1.control
- https://www.letianbiji.com/java-mockito/mockito-spy.html