Emma code coverage with JUnit and Powermock

£可爱£侵袭症+ 提交于 2019-12-10 21:31:31

问题


I am using JUnit with Powermockito mocking. I have to work on a CLI environment with maven or ant.

emma version:      ema-2.0.5312
powermock version: powermock-mockito-1.5.1-full
junit version:     junit-4.9

When I run junit through the following command, everything works find:

java org.junit.runner.JUnitCore some.package.ClassTest

However, when I used emma to check the code coverage:

java emmarun -cp $CLASSPATH -report txt org.junit.runner.JUnitCore some.package.ClassTest

I got the following error:

1) initializationError(some.pakage.ClassTest)
   java.lang.ClassCastException: org.powermock.modules.junit4.PowerMockRunner cannot be cast to org.junit.runner.Runner

Other test classes without using powermock work fine. Does anyone have some suggestion to this? thanks in advance.


回答1:


while using powermock, you can not find out the coverage using Emma

See this discussion on developer's side




回答2:


You can use MockitoJunitRunner and specify a rule to use PowerMock since Eclemma works along with MockitoJUnitRunner.

Something like this:

@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.

}

Dependencies needed:

    <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

Also you need to add this to the configurations under Coverage As -> Coverage Configurations -> Arguments.

Inside VM Arguments add -noverify and save.

for this to work with Jacoco use the following statement in your 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>


来源:https://stackoverflow.com/questions/17957496/emma-code-coverage-with-junit-and-powermock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!