0

I am using jacoco offline instumentation but dont see code coverage in the report for private function- getName(). When i run the jacoco reports, the coverage for getName() is 0%. Is there something wrong with the Junit or the configurations?

Below is the main class

public class CheckNameHandler implements RequestHandler<S3Event, Void> {
   @Override
   public Void handleRequest(S3Event s3Event, Context context) {
    String name = getName();
    return null;
  }
   private String getName(){
    return "test";
   }
}

Below is the test class

public class CheckNameHandlerTest {
 @Test
 public void handlerSuccessTest() {
  CheckNameHandler spyCheckNameHandler = PowerMockito.spy(new CheckNameHandler());
  PowerMockito.doReturn("test").when(spyCheckNameHandler, "getName");
  S3Event s3Event = .......;
  Context context = .......;
  spyCheckNameHandler.handleRequest(s3Event, context);
}

Below is the pom.xml

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.8</version>
                <executions>
                    <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>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <systemPropertyVariables>
                        <jacoco-agent.destfile>${project.build.directory}/coverage.exec</jacoco-agent.destfile>
                    </systemPropertyVariables>
                </configuration>
            </plugin>
        </plugins>
    </build> 

And below are the dependencies

<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-api-mockito2</artifactId>
    <version>1.7.4</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.powermock</groupId>
    <artifactId>powermock-module-junit4</artifactId>
    <version>1.7.4</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.jacoco</groupId>
    <artifactId>org.jacoco.agent</artifactId>
    <version>0.7.8</version>
    <scope>test</scope>
</dependency>

NKZ
  • 9
  • 4
  • Does this answer your question? [JaCoCo + Mockito + Android tests: Zero coverage reported](https://stackoverflow.com/questions/46517471/jacoco-mockito-android-tests-zero-coverage-reported) – Godin Aug 08 '21 at 22:12
  • No it doesnot. Jacoco reports the code coverage for handleRequest as 100% but doesnot show any coverage for getName(). How do i get complete coverage for the class without writing test case for private method (getName()) – NKZ Aug 09 '21 at 00:09
  • Please read my answer in https://stackoverflow.com/a/46614216/244993 carefully - similarly to it IMO there is clearly something wrong with your expectations/understanding about core thing here - mocking: you're mocking `getName` - `PowerMockito.doReturn("test").when(spyCheckNameHandler, "getName");` so why do you expect non zero coverage for it? – Godin Aug 09 '21 at 09:13

0 Answers0