Can I use Cobertura on Unit Tests with PowerMock?
Asked Answered
U

1

83

Problem

I am setting-up unit-test code coverage for an Android library which uses Robolectric to run the tests and PowerMock/Mockito for mock-testing.

However, running unit-tests with Cobertura results in the following Exception...

:example:testDebugUnitTest
Exception in thread "Thread-5" java.lang.ExceptionInInitializerError
    at com.example.package.saas.Query$RemoveWordsType.__cobertura_init(Query.java)
    at com.example.package.saas.Query$RemoveWordsType.<clinit>(Query.java)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at net.sourceforge.cobertura.coveragedata.TouchCollector.applyTouchesToSingleClassOnProjectData(TouchCollector.java:123)
    at net.sourceforge.cobertura.coveragedata.TouchCollector.applyTouchesOnProjectData(TouchCollector.java:110)
    at net.sourceforge.cobertura.coveragedata.ProjectData.saveGlobalProjectData(ProjectData.java:272)
    at net.sourceforge.cobertura.coveragedata.SaveTimer.run(SaveTimer.java:33)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: Shutdown in progress
    at java.lang.ApplicationShutdownHooks.add(ApplicationShutdownHooks.java:66)
    at java.lang.Runtime.addShutdownHook(Runtime.java:211)
    at net.sourceforge.cobertura.coveragedata.ProjectData.initialize(ProjectData.java:239)
    at net.sourceforge.cobertura.coveragedata.ProjectData.getGlobalProjectData(ProjectData.java:209)
    at net.sourceforge.cobertura.coveragedata.TouchCollector.<clinit>(TouchCollector.java:45)
    ... 11 more

...and the generated Cobertura report shows no coverage at all. Cobertura report with PowerMock


Running the same testcase without PowerMock*, the tests run fine and the coverage report is generated successfully: Cobertura report without PowerMock

​* i.e. commenting the tests using PowerMock, removing the PowerMockIgnore annotation, the PowerMockRule and the MockitoAnnotations.initMocks(this); invocation.


Investigation


Question

Is it possible to use Cobertura in conjunction with PowerMock?

  • In that case, what am I missing?
  • Otherwise, how should I measure code coverage with such a setup (Android Library + Robolectric + PowerMock)?
Underdrawers answered 29/3, 2016 at 13:3 Comment(6)
I'm having the same problem. Gradle build, Powermock tests show up with 0% coverage. Would love to get this solved.Westerly
+1 for your very clear question. Could you share a basic project reproducing the problem (on github for example) ?Fraga
In you test cases are you extending class from PowerMockTestCase?Vintager
@gontard: I can do so, but it would take some time to extract the relevant parts from the current project. Would it help you troubleshoot this? I could also create gists of the buildscript and of a testcase if this is what you would like to see.Underdrawers
@MoralesBatovski: Not really, as I am following robolectric's instructions (github.com/robolectric/robolectric/wiki/Using-PowerMock). Would you advise another approach?Underdrawers
I agree with @Fraga that a POC would be great. Since that's time consuming, can you add to the question your test class meanwhile? That'd also help troubleshooting this!Wrangler
B
1

Right now, Android Studio integrates jacoco automatically to do code coverage.

You just need to add a few lines of code:


apply plugin: 'jacoco-android'

android {
   buildTypes {
      debug {
         testCoverageEnabled = true
      }
   }
}

More information here:

Bannerol answered 15/5, 2016 at 11:51 Comment(1)
I tried your solution, but adding jacoco and running createDebugCoverageReport generates [an empty report]( i.sstatic.net/ACTcY.png). Apart from that, are you sure jacoco works for unit-tests (test) and not just for Instrumentation Testing (androidTest)? The article you linked does the latter, while I am trying to achieve the former with Robolectric.Underdrawers

© 2022 - 2024 — McMap. All rights reserved.