elcEmma与powermock整合生效

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

                 //      -ea -noverify -javaagent:lib/powermock-module-javaagent-1.6.0.jar

                 //agent 使用版1.6.0

And you should be able to use the Coverage As for EclEmma and see you test coverage result!



原文:
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)

 public class TestCaller {

@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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值