- 浏览: 2101957 次
- 性别:
- 来自: 北京
-
文章分类
- 全部博客 (366)
- hadoop (91)
- DB (2)
- vmware (0)
- python (49)
- Java (72)
- Linux (57)
- 多线程 (2)
- hive (1)
- hbase (3)
- mongodb (5)
- Hibernate (3)
- nodejs (1)
- 模式与重构 (1)
- 持续集成CI (4)
- Mysql (2)
- chorme (1)
- 软件开发 (0)
- 敏捷 (5)
- maven (3)
- windows (7)
- 测试驱动 (1)
- scala (3)
- Jetty (0)
- android (1)
- eclipse (1)
- 设计模式 (1)
- 面向对象设计 (2)
- oracle (3)
- cassandra (15)
- pig (3)
- rails (1)
- redis (4)
- ruby (1)
- storm (0)
- 实时运算 (3)
- scribe (1)
- hadoop源码 (3)
- cassandra jna (1)
- Kafka (2)
- Sublime (2)
- C++ (2)
- C (1)
- 版本控制 (1)
- 服务器 (1)
- docker (0)
- flink (1)
最新评论
-
伍大都督:
解释太到位了,感谢分享
理解Linux系统中的load average(图文版) -
rfjian123:
非常感谢,用你的方法解决了问题。
Server sent unexpected return value (403 Forbidden) in response to OPTIONS -
yuhaifei12:
今天遇到同样的问题了。设置的是每分钟执行一次。结果发现每分钟执 ...
解决Linux下crontab同一时间重复执行问题 -
BigBird2012:
想问一下,使用ExecutorService每次都要调用 sh ...
spring quartz使用多线程并发“陷阱” -
zhuqx1130:
有用,谢谢
解决Sublime Text 3中文显示乱码(tab中文方块)问题
原文在这里:http://tarunsapra.wordpress.com/2011/07/31/mocking-static-void-calls-with-powermock-junit/
Sometimes you do come across snippets of code that prove to be tricky while writing their JUnit tests. One such scenario is the case of static void call, though some people would argue to extract the static void call into a separate method but that is old-school. PowerMock junit runner lets you even mock static void and normal static calls. Let us take a look at both approaches the old one and the new one which uses PowerMock.
Older Approach
01 |
public class MyAction {
|
02 |
03 |
public String performSearch() {
|
04 |
05 |
// some logic operations |
06 |
07 |
// the detestable static call |
08 |
staticCall(); |
09 |
10 |
return "Success" ;
|
11 |
} |
12 |
13 |
protected void staticCall() {
|
14 |
// <strong>the static void method call</strong> |
15 |
MyStatic.staticMethod(); |
16 |
} |
17 |
18 |
} |
19 |
20 |
class MyStatic {
|
21 |
22 |
public static void staticMethod() {
|
23 |
throw new NullPointerException( "Bazinga!" );
|
24 |
25 |
} |
26 |
27 |
} |
In the above code snippet we can see that the static void call is extracted out in a separated method, now while writing the JUnit test for MyAction java class we can have anonymous sub-class which overrides the staticCall() method with it’s own implementation to avoid calling this method. Below code snippet elucidates the overriding
01 |
public class MyActionTest {
|
02 |
03 |
MyAction action; |
04 |
05 |
@Before |
06 |
public void setUp() {
|
07 |
action = new MyAction(){
|
08 |
protected void staticCall() {
|
09 |
10 |
System.out.println( "inside overridden method" );
|
11 |
}; |
12 |
}; |
13 |
} |
In the above snippet we can see that I have overridden the staticCall() method, now while executing the Junit test the super-class method containing the static call is never invoked and hence our purpose is solved of avoiding the static call. But the reason I don’t consider this approach clean is that our code should not be changed in order to make our Junit tests easier to write. Junit test should test existing running code without the need of modifying the code to meet JUnit’s need.
Now lets use PowerMock to mock the static void calls.
01 |
@RunWith (PowerMockRunner. class )
|
02 |
@PrepareForTest (MyStatic. class )
|
03 |
public class MyActionTest {
|
04 |
MyAction action; |
05 |
06 |
@Before |
07 |
public void setUp() {
|
08 |
action = new MyAction();
|
09 |
} |
10 |
11 |
@Test |
12 |
public void testPerformSearch() {
|
13 |
14 |
PowerMockito.spy(MyStatic. class );
|
15 |
PowerMockito.doNothing().when(MyStatic. class );
|
16 |
MyStatic.staticMethod(); |
17 |
Assert.assertEquals( "Success" , action.performSearch());
|
18 |
} |
19 |
} |
In the above code we have used PowerMock’s spy feature to partially mock the object, once you partial mock the object you have to stub the behavior i.e. prepare some canned calls. Here the PowerMock’s ability lies in the below three statements
1 |
PowerMockito.spy(MyStatic. class );
|
2 |
PowerMockito.doNothing().when(MyStatic. class );
|
3 |
MyStatic.staticMethod(); |
First we partially mock the MyStatic Class, then in the second line the doNothing() method is used to specify that the A static method call by the class MyStatic should result in doing nothing. Now since the object is partially mocked thus we also need to specify the method whose invocation is canned by doNothing() method, we do this in the 3rd statement MyStatic.staticMethod() , thus now it’s more of record-playback pattern wherein we have initially recorded the doNothing() behavior and then played the MyStatic.staticMethod().
Conclusion The PowerMock approach is best possible solution i could find for mocking static calls without changing actual code, if you have any other tricks up your sleeve do share them!
You can download the powermock mocktio jars zip from here
发表评论
-
shell下使用log4j 1.x “No appenders could be found for logger”问题两个处理办法
2018-05-25 23:25 1666错误: log4j:WARN No appenders c ... -
通过java class文件确定其编译器版本
2016-06-23 10:45 1089方法一: hexdump -C XXX.class ... -
字符编码笔记:ASCII,Unicode和UTF-8(转)
2014-11-06 17:29 1249今天中午,我突然想搞 ... -
Server sent unexpected return value (403 Forbidden) in response to OPTIONS
2014-10-10 15:01 5574之前用的好好的,忽然某天再次svn up时候就报下面的错误: ... -
netbeans下优秀sublinme主题
2014-09-29 10:13 3652推荐站点:http://netbeansthemes.com ... -
将netbeans项目放到jenkins上做持续集成
2014-07-25 15:20 1247netbeans项目本质是通过ANT来管理的,只不 ... -
关于jvm中Xmx参数默认值
2014-05-08 09:19 2034我的机器win7 64bit 8GB内存,通过jconso ... -
Eclipse代码自动完成功能无法使用问题记录
2014-04-28 10:15 1168使用Alt+/无法补齐Java代码,现象时没有反应。解决方 ... -
thrift0.9.1简单教程(包含Java服务端和Java、python客户端)
2014-04-25 15:55 6443一、Thrift Java服务端和客户端 官 ... -
使用sublime text3开发scala
2014-04-04 16:49 3817打开菜单栏Tool->build system-> ... -
netbeans下打开本地文件夹插件(explorer)
2014-04-02 14:44 20451. 在线安装 工具->插件->可用插 ... -
关闭Sublime自动更新
2014-03-09 21:42 874每次启动都提示更新,可以关闭它。 找到Prefere ... -
使用log4j.properties配置slf4j输出LOG
2014-01-09 08:57 6707完成配置需要以下文件: slf4j-log4j12-x ... -
正确使用java -cp通配符
2013-11-21 17:05 10222JDK6支持java -cp后面跟通配符'*',试了一下发 ... -
强制Java使用东八时区方法
2013-11-15 15:37 1859今天线上有台服务器时区错误,导致很多使用new Da ... -
谨慎使用java的PrintWriter类
2013-10-23 12:31 1381public void test() throws Fil ... -
记录Java ShutdownHook
2013-08-30 11:40 1083public class TestMe { stat ... -
eclipse + maven + jetty + spring web 开发环境简要笔记
2013-08-27 11:25 1624环境准备 确保安装maven3 确保安装eclip ... -
Eclipse(IDE for Java Developers)、maven、jetty、spring web集成
2013-08-27 08:53 0方法一: http://wiki.eclipse.o ... -
关于数据压缩
2013-08-23 14:26 1146常用压缩 quicklz zlib snappy/ ...
相关推荐
PowerMock允许我们使用`@PrepareForTest`注解指定需要模拟的类,并通过`PowerMockito.mockStatic`或`Mockito.when`来模拟私有方法。 下面是一段使用PowerMock模拟静态方法和私有方法的示例代码: ```java import ...
import static org.powermock.api.mockito.PowerMockito.*; @Test public void testWithMockedStaticMethod() { mockStatic(Math.class); when(Math.random()).thenReturn(0.5d); // 设置随机数返回值为0.5 // ...
赠送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-...
3. 使用PowerMock创建需要模拟的对象,例如`PowerMockito.mockStatic(YourClass.class)`。 4. 使用EasyMock或Mockito定义模拟对象的行为和期望。 5. 使用JUnit的@Test注解编写测试用例,运行测试。 总的来说,...
PowerMock-Mockito-JUnit-1.6.2.zip 是一个包含PowerMock、Mockito和JUnit集成库的压缩包,适用于Java开发环境。这个版本的PowerMock要求使用1.6或更高版本的Java Development Kit (JDK)。下面将详细解释这三个主要...
Instant Mock Testing with PowerMock 7 Saying Hello World! (Simple) 8 Getting and installing PowerMock (Simple) 14 Mocking static methods (Simple) 22 Verifying method invocation (Simple) 28 Mocking ...
PowerMock-Mockito-JUnit-1.6.3.zip 是一个包含最新版本的 PowerMock、Mockito 和 JUnit 库的压缩文件。这些工具在Java开发中被广泛用于单元测试和模拟,尤其对于处理静态方法、构造器、final类和方法、私有方法等...
5. 在测试方法中,使用`PowerMockito.mockStatic()`模拟静态方法,`when()`定义期望行为,`thenReturn()`等方法指定返回值。 6. 执行实际的测试代码,并在测试方法后使用`verify()`检查mock对象的行为是否符合预期。...
PowerMock 提供了 `PowerMockito.mockStatic` 方法,允许我们指定静态方法的行为,从而在测试中隔离静态方法的依赖。 3. **构造函数模拟**: 有些情况下,被测试类可能依赖于私有构造函数或不可实例化的类。...
例如,使用PowerMock模拟难以测试的部分,用Mockito定义mock对象的行为,最后由JUnit执行并报告测试结果。通过这种方式,开发者可以确保代码的质量和可靠性,提高软件的稳定性。同时,这个版本的测试套件也意味着它...
标签中的"powermock"、"jar"、"mock"、"mockito"和"junit"揭示了相关知识点: 1. **PowerMock**:这是核心概念,是一个Java单元测试框架,可以模拟那些通常无法被Mock的对象,如静态方法、final类等。 2. **Jar**...
3. **模拟静态方法**:使用`PowerMockito.mockStatic(Class<?> clazz)`来模拟静态方法。然后通过`when`和`thenReturn`(或其他方法)来定义静态方法的行为。 4. **模拟私有方法**:首先,需要使用`PowerMockito.spy...
【标题】"powermock-module-junit4-1.6.5.zip" 提供的是 PowerMock 模块的一个版本,这是 JUnit 测试框架的一个扩展工具。PowerMock 是一个强大的库,它允许开发者模拟静态方法、构造函数、删除 final 类和方法、...
赠送jar包:powermock-module-junit4-common-2.0.9.jar; 赠送原API文档:powermock-module-junit4-common-2.0.9-javadoc.jar; 赠送源代码:powermock-module-junit4-common-2.0.9-sources.jar; 赠送Maven依赖信息...
这个压缩包“PowerMock.zip”可能包含PowerMock库的jar文件和其他相关资源,帮助开发者在他们的项目中集成和使用PowerMock。 PowerMock 的核心功能在于它的模拟能力。在单元测试中,我们通常希望隔离被测试代码,只...
此外,集成测试框架如MockitoJUnitRunner或PowerMock可以帮助我们在JUnit中更容易地使用模拟对象。 测试驱动开发(TDD)是一种开发实践,提倡先写测试,再编写满足这些测试的代码。这种模式有助于确保代码质量,...
除了Mockito,Java社区还提供了其他mock工具,例如PowerMock,它扩展了Mockito的功能,允许我们模拟静态方法、构造函数、final类和方法,甚至私有方法,这些在标准Mockito中通常是不允许的。 在实际项目中,mock...