1. 程式人生 > >PowerMock單元測試

PowerMock單元測試

cep nbsp eth getconf 返回 light mod 介紹 moc

在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);

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));
	}
	
}

PowerMock單元測試