jacoco-android-gradle-plugin icon indicating copy to clipboard operation
jacoco-android-gradle-plugin copied to clipboard

PowerMockito @PrepareForTest disables JaCoCo

Open mmert1988 opened this issue 6 years ago • 5 comments

When using PowerMock in the unit tests, the classes declared with @PrepareForTest don't get coverage info due to following error:

[ant:jacocoReport] Classes in bundle 'app' do no match with execution data. For report generation the same class files must be used as at runtime.
[ant:jacocoReport] Execution data for class com/example/android/ExampleClassA does not match.
[ant:jacocoReport] Execution data for class com/example/android/ExampleClassB does not match.

After a small research I've found out that it's a known issue of jacoco regarding javassist/javaagent:

https://github.com/powermock/powermock/issues/422 https://github.com/jacoco/eclemma/issues/15

But unfortunately, I wasn't able to make the fix of the issue, as suggested for maven by using the custom java agent of PowerMock library, to work for gradle, because I couldn't declare a jar file from dependencies (powermock-api-mockito2-1.7.4.jar) as a custom java agent in gradle, as done in this workaround:

http://stevendick.github.io/blog/2012/02/12/jacoco-and-gradle-part-2/

It failed, since the java plugin is conflicting with the android plugin.

mmert1988 avatar Jul 11 '18 15:07 mmert1988

I found a workaround to this issue. Basically, you'll need to create a new Test-class for the class prepared for test by Powermock. This will reset any changes made by PowerMock to the class.

sachinlohith avatar May 03 '19 02:05 sachinlohith

I found a workaround to this issue. Basically, you'll need to create a new Test-class for the class prepared for test by Powermock. This will reset any changes made by PowerMock to the class.

Could you provide an example with working code?

Thanks, John

johnbrown8976 avatar Sep 04 '19 19:09 johnbrown8976

I found a workaround to this issue. Basically, you'll need to create a new Test-class for the class prepared for test by Powermock. This will reset any changes made by PowerMock to the class.

any working code ?

ghost avatar Jan 25 '20 19:01 ghost

Could you provide an example with working code?

allefsousa avatar Feb 19 '20 06:02 allefsousa

Could anyone please provide workaround for this?

Poojashivaswamy avatar May 12 '22 22:05 Poojashivaswamy