Problem with PowerLock ECLEmma


We use EasyMock and PowerMock with JUnit. The coating tool used is ECLEmma. With EasyMock, it correctly displays coverage in green (as described). However, for code that has been tested by the module with PowerMock, coverage is displayed in red (uncovered). Read similar questions on the Internet. However, I just wanted to check if there was a solution for this.

thank
Venkatesh

+21
java junit easymock
Apr 29 '14 at 11:19
source share
6 answers

This is a known issue: https://github.com/jayway/powermock/issues/422

And it was a long time ago, it will not be fixed in the near future.

I suggest using eCobertura instead .

+10
May 21 '14 at 10:00
source share

Yes, there is a solution for this:

First you need to add this maven dependency:

<dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4-rule-agent</artifactId> <version>1.6.4</version> <scope>test</scope> </dependency> 

Then, instead of using this @RunWith annotation (PowerMockRunner.class), simply add @Rule to the Test class as follows:

 public class Test { @Rule public PowerMockRule rule = new PowerMockRule(); 

You can find more on this blog How to get EclEmma testing to work with PowerMock

+11
Feb 24 '16 at 3:30
source share

In most cases, this worked in my project:

 @Rule public PowerMockRule rule = new PowerMockRule(); static { PowerMockAgent.initializeIfNeeded(); } 

Delete / Comment @RunWith(PowerMockRunner.class) and enable the following imports after adding powermock-module-javaagent-1.6.5.jar to your classpath:

 import org.junit.Rule; import org.powermock.modules.junit4.rule.PowerMockRule; import org.powermock.modules.agent.PowerMockAgent; 

Now right-click-> Coverage As-> Coverage Configurations and add the following lines to the Arguments:

 -ea -noverify -javaagent:path/to/powermock-module-javaagent-1.6.5.jar 

Click "Apply-> Coverage."

Also note that @Before will not work in this case, so you need to add all the materials in the methods marked with @Test with the method marked with @Before .

+4
Jun 15 '16 at 7:29
source share

We have static classes for layout. With mocking static classes, the eclEmma plugin for code coverage does not work in Eclipse. So what we did, therefore, placed @RunWith (JUnit4.class) (instead of @RunWith (PowerMockRunner.class)) in front of the class and placed the following lines inside the class

 static { PowerMockAgent.initializeIfNeeded(); } @Rule public PowerMockRule rule = new PowerMockRule(); 

Compiled the class and ran the test class. Code coverage works for the class. This change only occurs in the Eclipse environment.

After writing test cases, we returned the code to normal. Posted by @RunWith (PowerMockRunner.class) instead of @RunWith (JUnit4.class) and commented on the above static codes and powermockrule strings.

+2
Jan 31 '17 at 12:24
source share

I managed to create a PowerMock coverage with Jacoco using powermock-module-javaagent .

Just make sure you put the powermock agent after the jacoco agent:

 <artifactId>maven-surefire-plugin</artifactId> <configuration> <useSystemClassLoader>true</useSystemClassLoader> <argLine>${jacocoArgLine} -javaagent:${settings.localRepository}/org/powermock/powermock-module-javaagent/${powermock.version}/powermock-module-javaagent-${powermock.version}.jar -noverify</argLine> ... 

If you want to see an example, take a look at this project: https://github.com/jfcorugedo/sonar-scanner

Here you can see that sonar takes into account static methods and new operators modeled by PowerMock:

enter image description here

If you want PowerMockRule new operators, make sure you use PowerMockRule instead of PowerMockRunner .

Look at this test

+1
Aug 30 '18 at 22:29
source share

For mocking static classes, using @RunWith(PowerMockRunner.class) and running the โ€œJUnit Coverage Testโ€ in Eclipse really shows the covered code as uncovered, and this clearly seems to be a problem.

To add the solution above in the Maven project, you can try this ..

In the pom.xml root to generate reports, add html as the format in the cobertura-maven-plugin . Below it looks like this.

 <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>cobertura-maven-plugin</artifactId> <configuration> <formats> <format>html</format> <format>xml</format> </formats> </configuration> </plugin> 

Then go to the module where your class is located and open the target/site/cobertura/index.html file in the Eclipse web browser or in target/site/cobertura/index.html your choice. You can find coverage information there.

-one
Jul 20 '18 at 17:14
source share



All Articles