3

Code Coverage with Powermockito with jacoco

In my project we using powermockito, for codecoverage jacoco api. Seens we we use @preparefortest({xyzimpl.class,abcd.class}) annotation. On code coverage, xyzimpl.java code coverage is not covered. is there any work around to get codecoverage for above situation. Have read in other link say above annotation will not get codecoverage if we decleare class to be tested inside @preparefortest annotation.

KrishnaFJ
  • 155
  • 1
  • 2
  • 8

1 Answers1

4

Their documentation is pretty clear: it doesn't work (unless you get their "offline" instrumentation work - for which I have not seen working examples).

Due to its nature, PowerMock(ito) has a long history of not working with code coverage. Keep in mind: what @PrepareForTest is doing is: replace your product byte code with something that PowerMock generates.

In that sense, you only have these options:

  • if you absolutely need to mock static/new calls, then you could try to use JMockit (the only other framework that allows mocking calls to static/new)
  • learn how to write testable code - and avoid the need for such "special" mocking frameworks altogether. This will even improve the quality of your product.
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 1
    I'd be more clear, PowerMock does not replace code with generated. What it does - byte code modification. PowerMock wraps some of byte code instructions with `if-statement`. Like AspectJ or JaCoCo for example. – Artur Zagretdinov Aug 06 '17 at 20:45
  • As said elsewhere: it still has to *generate* something new to wrap around the existing byte code, right?! – GhostCat Aug 07 '17 at 11:29