在Java程式的單元測試中常用的mock工具有Mockito和EasyMock。但是這兩種mock工具都無法實作對靜态、final、私有方法或類的mock。是以有了功能強大的PowerMock工具。PowerMock并不是一個獨立、全新的工具而是在Mockito和EasyMock的基礎上進行的擴充,它分别有針對Mockito級EasyMock的擴充實作。本文主要介紹PowerMock的常見用法:
1.pom依賴
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10<version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.6.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.6.3</version>
<scope>test</scope>
</dependency>
2.基本用法
2.1 測試類加上必要的注解
@RunWith(PowerMockRunner.class)
@PrepareForTest({ExtensionLoader.class,Configs.class}) //靜态類,測試類本身(需要mock構造方法時)
@PowerMockIgnore("javax.management.*")
2.2 mock基本對象
測試 convert.convertAndHanlder(method,ref,bead)方法
@Mock
Convert convert;
when(convert.convertAndHanlder(eq(method),eq(ref),eq(bead))).thenReturn(bead);
2.3 mock無傳回值的方法
@Mock
ProviderComposeConfig providerComposeConfig;
doNothing().when(providerComposeConfig).putSync(eq(providerConfig));
2.4 mock方法執行異常場景
@Mock
ProviderComposeConfig providerComposeConfig;
doThrow(new Exception("執行失敗")).when(providerComposeConfig).putSync(eq(providerConfig));
2.5 mock 靜态方法
測試Configs.getConfig(ProviderComposeConfig.class)方法
前提:@PrepareForTest注解中添加該類:ProviderComposeConfig.class
@Mock
ProviderComposeConfig configs;
PowerMockito.mockStatic(Configs.class);
when(Configs.getConfig(eq(ProviderComposeConfig.class))).thenReturn(configs);
mock無傳回值的static方法
// "xxxUtil" 是類名
// "xxxStaticMethod" 是 static 方法的方法名
// 這裡假設 "xxxStaticMethod" 需要兩個參數,一個是 int 型,一個是 String 型
PowerMockito.doNothing().when(xxxUtil.class, "xxxStaticMethod",
mock可變參數傳遞
public class Demo {
public static void test(String... args) {
System.out.println(args);
}
}
測試方法:
@RunWith(PowerMockRunner.class)
@PrepareForTest({Demo.class})
@PowerMockIgnore("javax.management.*")
public class DemoTest {
@Test
public void test() throws Exception {
PowerMockito.mockStatic(Demo.class);
PowerMockito.doNothing().when(Demo.class,"test",eq("liu1"),eq("liu2"));
Demo.test("liu1","liu2");
}
}
2.6 mock 構造方法
前提:@PrepareForTest注解中添加該類:HookContext.class
@Mock
HookContext context;
//無參構造
PowerMockito.whenNew(HookContext.class).withNoArguments().thenReturn(context);
//有參構造
PowerMockito.whenNew(HookContext.class).withArguments(anystring()).thenReturn(context);
③完成執行個體
被測試類:
public class HandlerHook {
public final static String id = "handler";
@Override
public Bead doCall(HookContext context, Bead bead, AccessId id) throws Throwable {
ProviderComposeConfig configs = Configs.getConfig(ProviderComposeConfig.class);
ProviderConfig config = configs == null ? null : configs.get(id);
if (config == null) {
// 即沒有該服務,1.本地調用 2.用戶端資訊有誤,通路IP:PORT 卻沒有服務
return null;
}
// 拿到轉換器,調用
Convert convert = ExtensionLoader.getExtensionLoader(Convert.class).getExtensionById(ConvertProcessor.id);
return convert.convertAndHanlder(config.getMethod(), config.getRef(), bead);
}
}
測試類:
@RunWith(PowerMockRunner.class)
@PrepareForTest({ExtensionLoader.class,Configs.class})
@PowerMockIgnore("javax.management.*")
public class HandlerHookTest {
@Mock
HookContext hookContext;
@Mock
Bead bead;
@Mock
AccessId accessId;
@Mock
ExtensionLoader<Convert> extensionLoader;
@Mock
ProviderComposeConfig configs;
@Mock
ProviderConfig config;
@Mock
Method method ;
@Mock
Object ref;
@Mock
Convert convert;
private HandlerHook handlerHook = new HandlerHook();
@Test
public void doCall() throws Throwable {
PowerMockito.mockStatic(Configs.class);
when(Configs.getConfig(eq(ProviderComposeConfig.class))).thenReturn(configs);
when(configs.get(eq(accessId))).thenReturn(config);
PowerMockito.mockStatic(ExtensionLoader.class);
when(ExtensionLoader.getExtensionLoader(eq(Convert.class))).thenReturn(extensionLoader);
when(extensionLoader.getExtensionById(eq("map"))).thenReturn(convert);
when(config.getMethod()).thenReturn(method);
when(config.getRef()).thenReturn(ref);
when(convert.convertAndHanlder(eq(method),eq(ref),eq(bead))).thenReturn(bead);
assertEquals(bead, handlerHook.doCall(hookContext, bead, accessId));
}
}
轉載于:https://www.cnblogs.com/cowboys/p/9290950.html