I have been working on an android project and use roboletric and powermock to do unitTests.
When I run gradle jacocoTestReport
, it will show
[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/my/app/MyClass does not match.
Where I use powermock to mock the static method in Myclass.java
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
@PowerMockIgnore({ "org.mockito.*", "org.robolectric.*", "android.*" })
@PrepareForTest(MyClass.class)
public class TheTest {
@Rule
public PowerMockRule rule = new PowerMockRule();
@Test
public void test1() throws Exception {
PowerMockito.mockStatic(MyClass.class);
// do something
}
}
And the build.gradle is shown as follows
apply plugin: 'jacoco'
def coverageSourceDirs = [
'../app/src/main/java'
]
task jacocoTestReport(type:JacocoReport, dependsOn: "testDebugUnitTest") {
group = "Reporting"
description = "Generate Jacoco coverage reports"
classDirectories = fileTree(
dir: '../app/build/intermediates/classes/debug',
excludes: ['**/R.class',
'**/R$*.class',
'**/*$ViewInjector*.*',
'**/BuildConfig.*',
'**/Manifest*.*']
)
additionalSourceDirs = files(coverageSourceDirs)
sourceDirectories = files(coverageSourceDirs)
executionData = files('../app/build/jacoco/testDebugUnitTest.exec')
reports {
xml.enabled = true
html.enabled = true
}
}
I can still see the coverage report without distortion.
But How to get rid of such warning?
sonarqube
task, because my project is compiled with Java 8, jacoco test report is also generated with Java 8 and Sonarqube report is created with Java 11. I had to invokesonarqube
as follows:./gradlew sonarqube -x compileJava -x processResources -x classes -x jacocoTestReport -x compileTestJava -x test
to avoid the error. – Tamarau