`

PowerMock常见用法

阅读更多

 

import org.junit.Assert;  
import org.junit.Test;  
import org.junit.runner.RunWith;  
import org.mockito.Mockito;  
import org.powermock.api.mockito.PowerMockito;  
import org.powermock.core.classloader.annotations.PrepareForTest;  
import org.powermock.modules.junit4.PowerMockRunner;  
  
@RunWith(PowerMockRunner.class)  
@PrepareForTest({ MyClass.class})  
public class StaticClassSampleTest {  
  
	@Test  
	public void testPrivateMethod() throws Exception {  
		// 模拟 private的方法  
		MyClass spy = PowerMockito.spy(new MyClass());  
		PowerMockito.doReturn(3).when(spy, "private_method", 1);  
		Assert.assertEquals(3, spy.test_private_method(1));  
		PowerMockito.verifyPrivate(spy, Mockito.times(1)).invoke("private_method", 1);  
	}  
  
	@Test  
	public void testStaticReturnMethod() throws Exception {  
		// 模拟 静态有返回值的方法  
		PowerMockito.mockStatic(MyClass.class);  
		Mockito.when(MyClass.static_return_method()).thenReturn(2);  
		Assert.assertEquals(2, MyClass.static_return_method());  
	}  
  
	@Test  
	public void testVoidMethod() throws Exception {  
		// 模拟 不执行void的方法  
		MyClass spy = PowerMockito.spy(new MyClass());  
		PowerMockito.doNothing().when(spy).void_method();  
		spy.void_method();  
	}  
  
	@Test  
	public void testStaticMethod1() throws Exception {  
		// 模拟 不执行没参数的静态void的方法  
		PowerMockito.mockStatic(MyClass.class);  
		PowerMockito.doNothing().when(MyClass.class, "static_void_method");  
		MyClass.static_void_method();  
	}  
  
	@Test  
	public void testStaticMethod2() throws Exception {  
		// 模拟 不执行带参数的静态void的方法  
		PowerMockito.mockStatic(MyClass.class);  
		PowerMockito.doNothing().when(MyClass.class, "staticMethod", "123");  
		MyClass.staticMethod("123");  
  
		PowerMockito.doNothing().when(MyClass.class, "staticMethod", Mockito.anyString());  
		MyClass.staticMethod("456");  
	}  
  
}  
  
class MyClass {  
  
	final private int private_method(int a) {  
		return a;  
	}  
  
	public int test_private_method(int a) {  
		return private_method(a);  
	}  
  
	public static int static_return_method() {  
		return 1;  
	}  
  
	void void_method() {  
		throw new IllegalStateException("should not go here");  
	}  
  
	public static void static_void_method() {  
		throw new IllegalStateException("should not go here");  
	}  
  
	public static void staticMethod(String a) {  
		throw new IllegalStateException(a);  
	}  
}  

    maven依赖

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-all</artifactId>
    <version>1.8.5</version>
</dependency>
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>1.4.10</version>
</dependency>
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito</artifactId>
    <version>1.4.10</version>
</dependency> 

    另外补一下,测试目标方法内部对象的mock方式:

// 测试目标代码:
public class ClassUnderTest {
    public boolean callInternalInstance(String path) {
        File file = new File(path); 
        return file.exists(); 
    }
}
// 测试用例代码
@RunWith(PowerMockRunner.class) 
public class TestClassUnderTest{
    @Test 
    @PrepareForTest(ClassUnderTest.class) 
    public void testCallInternalInstance() throws Exception{
        File file = PowerMockito.mock(File.class); 
        ClassUnderTest underTest = new ClassUnderTest();
        PowerMockito.whenNew(File.class).withArguments("bbb").thenReturn(file);
        PowerMockito.when(file.exists()).thenReturn(true); 
        Assert.assertTrue(underTest.callInternalInstance("bbb")); 
    }
}

 

 *当某个测试方法被注解@PrepareForTest标注以后,
 在运行测试用例时,会创建一个新的org.powermock.
 core.classloader.MockClassLoader实例,
 然后加载该测试用例使用到的类(系统类除外)。
*PowerMock会根据你的mock要求,去修改写在
 注解@PrepareForTest里的class文件(当前测
 试类会自动加入注解中),以满足特殊的mock需求。
 例如:去除final方法的final标识,
 在静态方法的最前面加入自己的虚拟实现等。
*如果需要mock的是系统类的final方法和静态方法,
 PowerMock不会直接修改系统类的class文件,
 而是修改调用系统类的class文件,以满足mock需求。

 

分享到:
评论

相关推荐

    powermock

    powermock powermock powermock powermock

    powermock-core-2.0.9-API文档-中英对照版.zip

    使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。 双语对照,边学技术、边学英语。

    PowerMock实战

    全面介绍PowerMock的使用,帮助初学者彻底了解PowerMock的使用,提高代码的健壮性

    powermock-api-support-2.0.9-API文档-中英对照版.zip

    使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。 双语对照,边学技术、边学英语。

    powermock-api-mockito2-2.0.9-API文档-中英对照版.zip

    使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。 双语对照,边学技术、边学英语。

    汪文君powermock实战教学

    Powermock是基于现有的mock框架,easymock和mockito而建立的一个非常强大的框架,它具备easymock和mockito所不具备的一些功能,该系列教材详细的说明了如何使用powermock的所有特性

    powermock-classloading-xstream-1.4.7

    powermock-classloading-xstream-1.4.7powermock-classloading-xstream-1.4.7powermock-classloading-xstream-1.4.7powermock-classloading-xstream-1.4.7powermock-classloading-xstream-1.4.7powermock-class...

    powermock单元测试

    powermock1.4.9.jar和powermock测试静态方法和私有方法的测试代码,大家都知道easymock不能测试这些,配合使用和单元测试更加完善。

    powermock-module-junit4-2.0.9-API文档-中英对照版.zip

    使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。 双语对照,边学技术、边学英语。

    powermock maven respo

    powermock maven respo

    EasyMock and PowerMock入门PPT

    什么时候需要Mock对象 什么是EasyMock EasyMock的优点和缺点 运行EasyMock需要的资源 EasyMock的基本使用步骤 什么是PowerMock 运行PowerMock需要的资源 PowerMock的扩展功能

    powermock-cover.zip

    使用powermock+jacoco,测试静态方法,测试覆盖率。环境是:IDEA+maven。 直接执行:mvn clean install,然后查看覆盖率:powermock-cover\target\site\jacoco\index.html

    powermock-mockito-junit-1.6.2.zip

    powermock 版本要求比较严格,请使用1.6及以上jdk

    单元测试模拟框架PowerMock.zip

    通过提供定制的类加载器以及一些字节码篡改技巧的应用,PowerMock 现了对静态方法、构造方法、私有方法以及 Final 方法的模拟支持,对静态初始化过程的移除等强大的功能。因为 PowerMock 在扩展功能时完全采用和被...

    PowerMock实战手册

    PowerMock实战手册。采用的是Junit+PowerMock+Mockito这样的组合来进行讲述。

    powermock-module-junit4-common-2.0.9-API文档-中英对照版.zip

    赠送jar包:powermock-module-junit4-common-...使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。 双

    PowerMock.zip

    PowerMock资料,jar包 http://www.ibm.com/developerworks/cn/java/j-lo-powermock/

    powermock依赖jar文件.rar

    网上找了好多powermock的例子都不好用,一怒之下亲自挨个引入测试,最终得到这些jar,给需要的朋友,自己也留个备份。

    powermock-easymock-junit-1.5.4.zip

    powermock-easymock-junit-1.5.4.zip

    Instant_Mock_Testing_with_PowerMock

    tesunit junit easymock PowerMock

Global site tag (gtag.js) - Google Analytics