Jacoco showing 0% coverage even test cases are executed successfully when I am using @PrepareForTest annaotation.
Below are mine sample code. I want to test WebOmnibarLookupServiceImpl class public method loadWebOmnibarLookupCache(). in side its calling private method getWebOmnibarDictionaryKey.
@RunWith(PowerMockRunner.class)
@PrepareForTest({QueryRequestUpdater.class, WebOmnibarLookupServiceImpl.class})
public class WebOmnibarLookupServiceImplTest {
@InjectMocks
private WebOmnibarLookupServiceImpl service = new WebOmnibarLookupServiceImpl();
@Test
public void testloadWebOmnibarLookupCache() throws Exception {
WebOmnibarLookupServiceImpl privateService = PowerMock.createPartialMock(WebOmnibarLookupServiceImpl.class, "getWebOmnibarDictionaryKey");
PowerMock.expectPrivate(privateService, "getWebOmnibarDictionaryKey", TestData.getSearchQueryRequestDTOForCache()).andReturn(TestData.getWebOmnibarDictionaryKey());
doReturn(TestData.getSearchResponseObj()).when(webOmnibarDictionaryCache).get(any());
service.loadWebOmnibarLookupCache(TestData.getSearchQueryRequestDTOForCache());
}
}
Maven Configuration:
<profiles>
<profile>
<id>sonar-coverage</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.7.201606060606</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<append>true</append>
<excludes>
<exclude>**/*Config.*</exclude>
<exclude>**/*Constants.*</exclude>
<exclude>**/*Enum.*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>agent-for-ut</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>default-restore-instrumented-classes</id>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/coverage.exec</dataFile>
</configuration>
</execution>
<execution>
<id>agent-for-it</id>
<goals>
<goal>prepare-agent-integration</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Here I am using @@PrepareForTest({ WebOmnibarLookupServiceImpl.class}) to mock private method. Here test executed successfully but coverage showing 0% for this class only those class are not added @PrepareForTest showing coverage percentage. I am geeting below warning message while execute testcases.
[WARNING] Classes in bundle 'webanalytics-server' do no match with execution data. For report generation the same class files must be used as at runtime.
[WARNING] Execution data for class com/shn/webanalytics/service/impl/WebOmnibarLookupServiceImpl does not match.
I see that you are using PowerMock. There is a known issue of jacoco and powermock compatibility. You can find more details here.
One of the solution is you can do offline jacoco instrumentation on the code instead of on the fly instrumentation.
Here is one of the example done in maven for offline instrumentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With