使用JUnit和Powermock进行Emma代码覆盖

问题描述 投票:3回答:2

我正在使用JUnit和Powermockito模拟。我必须使用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的测试类工作正常。有没有人对此提出一些建议?提前致谢。

junit powermock emma
2个回答
1
投票

使用powermock时,你无法找到使用Emma的覆盖范围

See this discussion on developer's side


0
投票

您可以使用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

您还需要将其添加到Coverage As - > Coverage Configurations - > Arguments下的配置中。

在VM Arguments内添加-noverify和save.enter image description here

为了使用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>
© www.soinside.com 2019 - 2024. All rights reserved.