Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jacoco misses all coverage if @PrepareForTest is used [duplicate]

I am trying to get the code coverage report on the sonarqube dashboard on jenkins. The code coverage report is coming up but showing only 4.6% coverage. On investigating I found out that the test classes written using PowerMocks are getting skipped.

On further investigation I found that "JaCoCo doesn't play well with dynamically modified/created classes (this is the way how powermock works). This is a known limitation we can't currently do anything about".

Is there any work around for this so that I can get proper code coverage for test classes written using PowerMocks too.

like image 782
Manas Avatar asked May 09 '17 11:05

Manas


People also ask

Why does JaCoCo not show coverage?

Why does the coverage report not show highlighted source code? Make sure the following prerequisites are fulfilled to get source code highlighting in JaCoCo coverage reports: Class files must be compiled with debug information to contain line numbers. Source files must be properly supplied at report generation time.

Does JaCoCo working with PowerMock?

JaCoCo Offline Instrumentation works only with PowerMock version 1.6. 6 and above. You may find example of using PowerMock with JaCoCo Offline Instrumentation and Maven in our repository: jacoco-offline example.

How do I get code coverage with PowerMock?

Put your powermock test logic in other simple java class inside test package, and call the merhod from test class, it increase code coverage for your application.

How does JaCoCo determine coverage?

JaCoCo mainly provides three important metrics: Lines coverage reflects the amount of code that has been exercised based on the number of Java byte code instructions called by the tests. Branches coverage shows the percent of exercised branches in the code, typically related to if/else and switch statements.


2 Answers

I have managed to generate PowerMock coverage with Jacoco, using powermock-module-javaagent.

Just make sure you put powermock agent after 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 statements mocked by PowerMock:

enter image description here

If you want to mock newstatements make sure you use PowerMockRule instead of PowerMockRunner.

Take a look at this test

like image 122
jfcorugedo Avatar answered Sep 24 '22 19:09

jfcorugedo


Simple answer: no, there isn't.

Long answer - boils down to these options:

  • have look into this Wiki page by the PowerMock team - maybe maybe "offline instrumentation" works out for you.
  • hope that the corresponding bug gets fixed at some point (I wouldn't hold my breath on that)
  • get rid of your dependency to PowerMock(ito) - by refactoring and improving your production code
  • [ I think I evaluated various coverage tools long time ago; and there was one commercial one that claims to work even with PowerMock. But I don't recall any specifics. So I am basically saying: there might be a minuscule chance that another, proprietary coverage tool works with PowerMock ]
like image 28
GhostCat Avatar answered Sep 24 '22 19:09

GhostCat