Knowledgebase‐Diffblue Cover tools - saayam-for-all/docs GitHub Wiki
Use Diffblue Cover tools to generate JUnit automatically
We need to add the JUnit dependency and the jacoco-maven-plugin in the pom.xml.
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
</dependency>
<!-- Jacoco plugin -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<!-- Sets the path to the file which contains the execution data. -->
<dataFile>target/jacoco.exec</dataFile>
<!-- Sets the output directory for the code coverage report. -->
<outputDirectory>target/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
In IntelliJ, go to the Plugins and install Diffblue Cover.
After restarting IntelliJ, go to the Java development directory, choose the Java class you want to test, right-click it, and select “Write Tests” from the menu. Diffblue Cover will automatically generate tests for the existing methods in the class.
Once the tests are generated, you can find the corresponding JUnit test files in the test directory.
Finally, in the Terminal run mvn clean test
to execute the covertage tests. I have configured the jacoco-maven-plugin
in the pom.xml
to output the coverage report to the target/jacoco-ut
directory. You can view the test report by using the command open target/jacoco-ut/index.html
.