3

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.

wlhee
  • 2,334
  • 4
  • 18
  • 19
  • 1
    possible duplicate of [PowerMock ECLEmma coverage issue](http://stackoverflow.com/questions/23363212/powermock-eclemma-coverage-issue) – Duc Tran Oct 10 '14 at 01:45

2 Answers2

1

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

See this discussion on developer's side

ankit
  • 4,919
  • 7
  • 38
  • 63
0

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.enter image description here

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>