Knowledgebase‐Diffblue Cover tools - saayam-for-all/docs GitHub Wiki

Use Diffblue Cover tools to generate JUnit automatically

1. Add the JUnit dependency and jacoco-maven-plugin

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>

2. Install Diffblue Cover

In IntelliJ, go to the Plugins and install Diffblue Cover.

image

3. How to use Diffblue Cover to generate JUnit tests

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.

image

4. Automatically generate JUnit code

Once the tests are generated, you can find the corresponding JUnit test files in the test directory.

image

5. Review coverage test report

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.

image image
⚠️ **GitHub.com Fallback** ⚠️