elcEmma与powermock整合生效
来源:互联网 发布:中国的乒乓球知乎 编辑:程序博客网 时间:2024/06/06 17:49
Make EclEmma test coverage work with PowerMock
5 Replies
EclEmma and Mockito do not work well together out-of-the-box. EclEmma report no coverage at all on test run with PowerMock runner. However there is a way to make it work together, using the PowerMock javaagent. To do this,don’t use the @RunWith(PowerMockRunner.class) annotation as you’re used to. Instead, use the standard JUnit Runner or your runner extending a JUnit one, and add this rule to all your test classes:
@Rule
public PowerMockRule rule = new PowerMockRule();
You will also need to add the 2 following jars to your classpath, as explained here:
powermock-module-javaagent
powermock-module-junit4-rule-agent
You can find then on the Maven repository: here and here.
Third step is to initialize the agent, add the following lines to all your test classes, or to you runner if you have one to add it only once:
static {
PowerMockAgent.initializeIfNeeded();
}
And finally last step: modify your launch configuration running your test to add the following option to the VM arguments:
-javaagent:lib/powermock-module-javaagent-1.6.0.jar
// -javaagent:<jarpath>[=<options>]
// load Java programming language agent, see java.lang.instrument
原文:
http://www.notonlyanecmplace.com/make-eclemma-test-coverage-work-with-powermock/
---------------我是分割线-------------------
Bootstrapping using a JUnit Rule
Since version 1.4 it's possible to bootstrap PowerMock using a JUnit Rule instead of using the PowerMockRunner and the RunWith annotation. This allows you to use other JUnit runners while still benefiting from PowerMock's functionality. You do this by specifying:
@PrepareForTest(X.class); //我实践发现该注解必须放到类之前
public class MyTest {
@Rule
PowerMockRule rule = new PowerMockRule();
// Tests goes here
...
}
原文
https://github.com/powermock/powermock/wiki/PowerMockRule
---------------我是分割线-------------------
//agent 使用版1.6.0
我的demo如下:
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.powermock.api.support.membermodification.MemberMatcher;
import org.powermock.api.support.membermodification.MemberModifier;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.agent.PowerMockAgent;
import org.powermock.modules.junit4.rule.PowerMockRule;
class Callee {
public String say(){
return "not replaced";
}
public static void main(String[] args){
System.out.println("hah");
}
}
class Caller {
private Callee callee = new Callee();
public Caller(){
System.out.println("caller");
}
public String say() {
return callee.say();
}
}
@Rule
public PowerMockRule rule = new PowerMockRule();
static {
PowerMockAgent.initializeIfNeeded();
}
public static void sayStub(){
System.out.println("replaced");
}
@Test
//@PrepareForTest(Callee.class)
public void testCaller(){
Caller caller = new Caller();
System.out.println(caller.say());
MemberModifier.stub(MemberMatcher.method(Callee.class, "say")).toReturn("replaced");
System.out.println(caller.say());
MemberModifier.stub(MemberMatcher.method(Callee.class, "say")).toReturn("replaced222");
System.out.println(caller.say());
sayStub();
}
public static void main(String[] args){
System.out.println("hah");
}
}
5 Replies
EclEmma and Mockito do not work well together out-of-the-box. EclEmma report no coverage at all on test run with PowerMock runner. However there is a way to make it work together, using the PowerMock javaagent. To do this,don’t use the @RunWith(PowerMockRunner.class) annotation as you’re used to. Instead, use the standard JUnit Runner or your runner extending a JUnit one, and add this rule to all your test classes:
@Rule
public PowerMockRule rule = new PowerMockRule();
You will also need to add the 2 following jars to your classpath, as explained here:
powermock-module-javaagent
powermock-module-junit4-rule-agent
You can find then on the Maven repository: here and here.
Third step is to initialize the agent, add the following lines to all your test classes, or to you runner if you have one to add it only once:
static {
PowerMockAgent.initializeIfNeeded();
}
And finally last step: modify your launch configuration running your test to add the following option to the VM arguments:
-javaagent:lib/powermock-module-javaagent-1.6.0.jar
// -javaagent:<jarpath>[=<options>]
// load Java programming language agent, see java.lang.instrument
// -ea -noverify -javaagent:lib/powermock-module-javaagent-1.6.0.jar
//agent 使用版1.6.0
原文:
http://www.notonlyanecmplace.com/make-eclemma-test-coverage-work-with-powermock/
---------------我是分割线-------------------
Bootstrapping using a JUnit Rule
Since version 1.4 it's possible to bootstrap PowerMock using a JUnit Rule instead of using the PowerMockRunner and the RunWith annotation. This allows you to use other JUnit runners while still benefiting from PowerMock's functionality. You do this by specifying:
@PrepareForTest(X.class); //我实践发现该注解必须放到类之前
public class MyTest {
@Rule
PowerMockRule rule = new PowerMockRule();
// Tests goes here
...
}
原文
https://github.com/powermock/powermock/wiki/PowerMockRule
---------------我是分割线-------------------
//agent 使用版1.6.0
我的demo如下:
package test;
import org.junit.Rule;
import org.junit.Test;import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.powermock.api.support.membermodification.MemberMatcher;
import org.powermock.api.support.membermodification.MemberModifier;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.agent.PowerMockAgent;
import org.powermock.modules.junit4.rule.PowerMockRule;
class Callee {
public String say(){
return "not replaced";
}
public static void main(String[] args){
System.out.println("hah");
}
}
class Caller {
private Callee callee = new Callee();
public Caller(){
System.out.println("caller");
}
public String say() {
return callee.say();
}
}
@RunWith(JUnit4.class)
@PrepareForTest(Callee.class)
@Rule
public PowerMockRule rule = new PowerMockRule();
static {
PowerMockAgent.initializeIfNeeded();
}
public static void sayStub(){
System.out.println("replaced");
}
@Test
//@PrepareForTest(Callee.class)
public void testCaller(){
Caller caller = new Caller();
System.out.println(caller.say());
MemberModifier.stub(MemberMatcher.method(Callee.class, "say")).toReturn("replaced");
System.out.println(caller.say());
MemberModifier.stub(MemberMatcher.method(Callee.class, "say")).toReturn("replaced222");
System.out.println(caller.say());
sayStub();
}
public static void main(String[] args){
System.out.println("hah");
}
}
0 0
- elcEmma与powermock整合生效
- Mockito与PowerMock
- powermock
- powermock
- powermock
- PowerMock与Mockito的使用
- PowerMock+SpringMVC整合并测试Controller层方法(一)
- shiro与spring整合详解与spring项目中shiro注解不生效的解决办法
- 窗体按钮生效与未生效问题
- spring4整合hibernate4事务不生效问题
- [单元测试]PowerMock+SpringMVC整合并测试Controller层方法(二)
- PowerMock用法
- PowerMock介绍
- PowerMock介绍
- PowerMock介绍
- PowerMock介绍
- PowerMock 简介
- PowerMock使用
- 查看mysql数据库、数据表编码信息
- 支付宝最新sdk接入总结
- 动态规划—18买书
- SSH的相关设置处理
- 堆排序代码实现
- elcEmma与powermock整合生效
- AlertDialog出现黑边快速解决方法
- jQuery入门杂谈
- An Energy-Efficient Ant-Based Routing Algorithm for Wireless Sensor Networks (无线传感网中基于蚁群算法的能量有效路由)2
- iOS Hacker 使用Theos开发tweak
- 【linux学习】mac启动ftp命令
- 在windows系统下安装Linux虚拟机
- EventBus使用详解(一)——初步使用EventBus
- Unity 编辑器ScrollView滚动卡顿优化