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

                 //      -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 0
原创粉丝点击