Code Coverage #120
-
Hi, I want to use a code coverage with our custom provider and your test container (with maven). I have : Someone used JaCoCo with test container ? Which code coverage do you use ? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I also faced the same issue and finally managed to make Jacoco work with test container. <!-- add jacoco agent -->
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<version>${jacoco.version}</version>
<classifier>runtime</classifier>
<scope>test</scope>
</dependency>
....
<!-- copy the jacoco agent to target build -->
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-jacoco</id>
<goals>
<goal>copy-dependencies</goal>
</goals>
<phase>compile</phase>
<configuration>
<includeArtifactIds>org.jacoco.agent</includeArtifactIds>
<includeClassifiers>runtime</includeClassifiers>
<outputDirectory>${project.build.directory}/jacoco-agent</outputDirectory>
<stripVersion>true</stripVersion>
</configuration>
</execution>
</executions>
</plugin>
<!-- set up jacoco plugin to generate report -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<id>report</id>
<phase>integration-test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco-report/jacoco.exec</dataFile>
</configuration>
</execution>
</executions>
</plugin> Then set up your Keycloak container to copy the Jacoco agent and make it runs when the container starts private static final KeycloakContainer keycloakContainer = new KeycloakContainer()
.withNetwork(network)
.withProviderClassesFrom("target/classes")
.withCopyFileToContainer(
MountableFile.forHostPath("target/jacoco-agent/"),
"/jacoco-agent"
)
.withEnv("JAVA_OPTS_APPEND", "-javaagent:/jacoco-agent/org.jacoco.agent-runtime.jar=destfile=/tmp/jacoco.exec"); In the tear down method, remember to stop the container by using DockerClient to create the dump file then copy to the target build directory void tearDown() {
....
keycloakContainer.getDockerClient().stopContainerCmd(keycloakContainer.getContainerId()).exec();
keycloakContainer.copyFileFromContainer("/tmp/jacoco.exec", "./target/jacoco-report/jacoco.exec");
keycloakContainer.stop();
} And you can get the coverage report after executing test in |
Beta Was this translation helpful? Give feedback.
I also faced the same issue and finally managed to make Jacoco work with test container.
Let me summarize my solution.
The idea is executing the Jacoco agent inside the Keycloak test container and copying the output dump file to host machine after finishing the test.
First, set up your pom file to add the Jacoco agent library