mock官网:https://github.com/powermock/powermock/wiki/SuppressUnwantedBehavior
—————————————抑制static{}———————————————————
Suppress static initializer
Some times a thrid-party class does something in its static initializer (also called static constructor) that prevents you from unit testing your own class. It's also possible that your own class does something in a static initializer which you don't want to happen when you unit test your class. PowerMock can then simply suppress the static initialization of that class. You do this by specifying the @SuppressStaticInitializationFor annotation at the class-level or method-level of the test. For example let's say you want to unit-test the following class:
public class ExampleWithEvilStaticInitializer {
static {
System.loadLibrary("evil.dll");
}
private final String message;
public ExampleWithEvilStaticInitializer(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
}
The problem here is that when the ExampleWithEvilStaticInitializer class is loaded the static code block will be executed and the System.loadLibrary("evil.dll") will be executed causing the unit test to fail (if the evil.dll cannot be loaded). To suppress this static initializer we do like this:
@SuppressStaticInitializationFor("org.mycompany.ExampleWithEvilStaticInitializer")
————————————————————————————————————————————
分享到:
相关推荐
powermock powermock powermock powermock
全面介绍PowerMock的使用,帮助初学者彻底了解PowerMock的使用,提高代码的健壮性
Powermock是基于现有的mock框架,easymock和mockito而建立的一个非常强大的框架,它具备easymock和mockito所不具备的一些功能,该系列教材详细的说明了如何使用powermock的所有特性
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...
赠送jar包:powermock-api-support-2.0.9.jar; 赠送原API文档:powermock-api-support-2.0.9-javadoc.jar; 赠送源代码:powermock-api-support-2.0.9-sources.jar; 赠送Maven依赖信息文件:powermock-api-support...
赠送jar包:powermock-api-mockito2-2.0.9.jar; 赠送原API文档:powermock-api-mockito2-2.0.9-javadoc.jar; 赠送源代码:powermock-api-mockito2-2.0.9-sources.jar; 赠送Maven依赖信息文件:powermock-api-...
赠送jar包:powermock-core-2.0.9.jar; 赠送原API文档:powermock-core-2.0.9-javadoc.jar; 赠送源代码:powermock-core-2.0.9-sources.jar; 赠送Maven依赖信息文件:powermock-core-2.0.9.pom; 包含翻译后的API...
powermock maven respo
什么时候需要Mock对象 什么是EasyMock EasyMock的优点和缺点 运行EasyMock需要的资源 EasyMock的基本使用步骤 什么是PowerMock 运行PowerMock需要的资源 PowerMock的扩展功能
赠送jar包:powermock-module-junit4-2.0.9.jar; 赠送原API文档:powermock-module-junit4-2.0.9-javadoc.jar; 赠送源代码:powermock-module-junit4-2.0.9-sources.jar; 赠送Maven依赖信息文件:powermock-...
powermock 版本要求比较严格,请使用1.6及以上jdk
学习powermock实战手册,自己写的若干例子,mock static、final ,mock 局部变量、成员变量、Answer接口、ArgumentMatcher
Mocking static methods (Simple) 22 Verifying method invocation (Simple) 28 Mocking final classes or methods (Simple) 34 Mocking constructors (Medium) 37 Understanding argument matchers (Medium) 42 ...
PowerMock 也是一个单元测试模拟框架,它是在其它单元测试模拟框架的基础上做出的扩展。通过提供定制的类加载器以及一些字节码篡改技巧的应用,PowerMock 现了对静态方法、构造方法、私有方法以及 Final 方法的模拟...
使用powermock+jacoco,测试静态方法,测试覆盖率。环境是:IDEA+maven。 直接执行:mvn clean install,然后查看覆盖率:powermock-cover\target\site\jacoco\index.html
PowerMock实战手册。采用的是Junit+PowerMock+Mockito这样的组合来进行讲述。
网上找了好多powermock的例子都不好用,一怒之下亲自挨个引入测试,最终得到这些jar,给需要的朋友,自己也留个备份。
PowerMock资料,jar包 http://www.ibm.com/developerworks/cn/java/j-lo-powermock/
powermock-easymock-junit-1.5.4.zip
PowerMock uses a custom classloader and bytecode manipulation to enable mocking of static methods, constructors, final classes and methods, private methods, removal of static initializers and more....