@TOC
前言
上篇講解了mock的簡(jiǎn)單實(shí)用丈积,初步了解了mock颂斜,今天我們來(lái)了解一下進(jìn)階的使用方式壁公。
一、Mock進(jìn)階使用
1.測(cè)試方法
/**
* @version 1.0
* @Description 方法mockDemo
* @Author 殘冬十九
* @Date 2020/6/12
*/
public class MockStaticDemo {
@Resource
private StaticDemoTarget staticDemoTarget;
/**
* 加法奈揍,求兩數(shù)之和
*
* @param a 數(shù)值A(chǔ)
* @param b 數(shù)值B
* @return 兩數(shù)之和
*/
public int addition(int a, int b) {
return a + StaticDemoTarget.multiply(b);
}
/**
* 加法曲尸,獲取a和ab中最大值的和,new出來(lái)的對(duì)象
*
* @param a 數(shù)值A(chǔ)
* @param b 數(shù)值B
* @return 兩數(shù)之和
*/
public int sum(int a, int b) {
return a + new StaticDemoTarget().max(a, b);
}
/**
* 加法男翰,獲取a和ab中最大值的和,引用的對(duì)象
*
* @param a 數(shù)值A(chǔ)
* @param b 數(shù)值B
* @return 兩數(shù)之和
*/
public int sumResource(int a, int b) {
return a + staticDemoTarget.max(a, b);
}
}
class StaticDemoTarget {
/**
* 乘法另患,將數(shù)值A(chǔ)乘以3并返回
*
* @param a 數(shù)值A(chǔ)
* @return 數(shù)值a乘以三之后的值
*/
public static int multiply(int a) {
return a * 3;
}
/**
* 獲取最大值
*
* @param a 數(shù)值A(chǔ)
* @param b 數(shù)值B
* @return 最大值
*/
public int max(int a, int b) {
return Math.max(a, b);
}
}
Mock方法
/**
* @version 1.0
* @Description mock實(shí)例
* @Author 殘冬十九
* @Date 2020/6/12
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({StaticDemoTarget.class, MockStaticDemo.class})
public class MockStaticDemoTest extends TestCase {
@InjectMocks
private MockStaticDemo mockStaticDemo;
@Mock
private StaticDemoTarget staticDemoTarget;
/**
* 靜態(tài)方法摸mock
*/
public void testAddition() {
//mock靜態(tài)類(lèi)
mockStatic(StaticDemoTarget.class);
//如果使用了anyInt()的測(cè)試條件,就代表任何數(shù)據(jù)都可以滿足條件蛾绎。返回5
when(StaticDemoTarget.multiply(anyInt())).thenReturn(5);
//下面進(jìn)行斷言
Assert.assertEquals(6, mockStaticDemo.addition(1, 2));
System.out.println("testAddition結(jié)束");
}
/**
* new 出對(duì)象mock
*
* @throws Exception
*/
public void testSum() throws Exception {
//設(shè)置最大值方法返回10
when(staticDemoTarget.max(anyInt(), anyInt())).thenReturn(10);
whenNew(StaticDemoTarget.class).withNoArguments().thenReturn(staticDemoTarget);
//下面進(jìn)行斷言
Assert.assertEquals(11, mockStaticDemo.sum(1, 2));
System.out.println("testSum結(jié)束");
}
/**
* 引用出來(lái)對(duì)象mock
*
* @throws Exception
*/
public void testSumResource() throws Exception {
// mock一個(gè)需要resource的對(duì)象
StaticDemoTarget staticDemoTarget = mock(StaticDemoTarget.class);
//將兌現(xiàn)賦值進(jìn)去
Whitebox.setInternalState(mockStaticDemo, "staticDemoTarget", staticDemoTarget);
//設(shè)置最大值方法返回10
when(staticDemoTarget.max(anyInt(), anyInt())).thenReturn(10);
whenNew(StaticDemoTarget.class).withNoArguments().thenReturn(staticDemoTarget);
//下面進(jìn)行斷言
Assert.assertEquals(11, mockStaticDemo.sumResource(1, 2));
System.out.println("testSumResource結(jié)束");
}
}
上圖中可以看出昆箕,我們有靜態(tài)方法,代碼中new出來(lái)的對(duì)象租冠,還有引用的對(duì)象三種mock方式鹏倘,這三種方法也是比較常用的方法。
需要注意的是PrepareForTest注解顽爹,需要mock靜態(tài)方法的時(shí)候纤泵。必須使用PrepareForTest注解,并且在注解后跟上需要mock靜態(tài)方法對(duì)應(yīng)的靜態(tài)類(lèi)镜粤。如果使用PrepareForTest注解捏题,就一定要配套使用RunWith注解才行。
InjectMocks注解是注解需要測(cè)試的類(lèi)肉渴,其他mock對(duì)象都會(huì)直接注入到這個(gè)類(lèi)中公荧。
希望寫(xiě)的文章對(duì)大家有幫助。