JUnit和Powermock的Emma代码覆盖率

JUnit和Powermock的Emma代码覆盖率,junit,powermock,emma,Junit,Powermock,Emma,我将JUnit与Powermockito mocking一起使用。 我必须使用maven或ant在CLI环境中工作 emma version: ema-2.0.5312 powermock version: powermock-mockito-1.5.1-full junit version: junit-4.9 当我通过以下命令运行junit时,一切正常: java org.junit.runner.JUnitCore some.package.ClassTest 但是,

我将JUnit与Powermockito mocking一起使用。 我必须使用maven或ant在CLI环境中工作

emma version:      ema-2.0.5312
powermock version: powermock-mockito-1.5.1-full
junit version:     junit-4.9
当我通过以下命令运行junit时,一切正常:

java org.junit.runner.JUnitCore some.package.ClassTest
但是,当我使用emma检查代码覆盖率时:

java emmarun -cp $CLASSPATH -report txt org.junit.runner.JUnitCore some.package.ClassTest
我得到了以下错误:

1) initializationError(some.pakage.ClassTest)
   java.lang.ClassCastException: org.powermock.modules.junit4.PowerMockRunner cannot be cast to org.junit.runner.Runner
没有使用powermock的其他测试类工作正常。
有人对此有什么建议吗?提前感谢。

使用powermock时,您无法使用Emma查找覆盖范围


您可以使用MockitoJunitRunner并指定使用PowerMock的规则,因为Eclemma与MockitoJunitRunner一起工作

大概是这样的:

@RunWith(MockitoJUnitRunner.class) // This supports Eclemma Plugin. Powermock doesn't.
@PrepareForTest({/* StaticClasses for Powermock here */})
public class ClassTest {

// These two statements; the static block and @Rule make sure Powermock works along with Mockito!!
static {
    PowerMockAgent.initializeIfNeeded();
}

@Rule
public PowerMockRule powerMockRule = new PowerMockRule();

@Mock // To mock dependent class
private MockClass mock;

@InjectMocks //To Inject all mocks in this class
private ClassUnderTest classObject;

//Rest of the code here.

}
需要依赖项:

    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito</artifactId>
        <version>1.6.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>1.6.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4-rule-agent</artifactId>
        <version>1.6.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.10.19</version>
        <scope>test</scope>
    </dependency

org.powermock

要使用Jacoco,请在pom.xml中使用以下语句

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <configuration>
    <argLine>${argLine} -noverify -javaagent:${settings.localRepository}/org/powermock/powermock-module-javaagent/1.6.2/powermock-module-javaagent-1.6.2.jar</argLine>
    </configuration>
</plugin>

org.apache.maven.plugins
maven surefire插件
2.18.1
${argLine}-noverify-javaagent:${settings.localRepository}/org/powermock/powermock模块javaagent/1.6.2/powermock-module-javaagent-1.6.2.jar