Knowledge Base > Legacy Information - JCover > Getting Started with JCover
Getting started with JCover
Diffblue’s JCover is a command-line tool for automatically generating unit tests for Java code, which can be easily incorporated into your existing Continuous Integration (CI) framework to catch software regressions before they reach production source.
- 1. Download JCover
- 2. Prerequisites
- 3. Installing JCover
- 4. Check you can run JCover
- 5. Writing unit tests with JCover using Maven
- 6. Next steps
1. Download JCover
Please contact Diffblue to obtain JCover.
2. Prerequisites
2.1 Software Prerequisites
- JDK 6 - 17 (Please note: a JRE is insufficent)
- JUnit 3.7 - 5 or TestNG 4.4.7 - 7.1.0
- Maven 3.6+, Gradle or Ant
2.2 Dependencies
Dependency | When | Version |
---|---|---|
JUnit (junit:junit or org.junit.jupiter:junit-jupiter-engine ) |
When using JUnit | 3.7 |
Spring Boot Test (org.springframework:spring-boot-test ) |
when using Spring Boot (org.springframework:spring-boot ) |
1+ or 2+, matching version |
Spring Test (org.springframework:spring-test ) |
when using Spring (org.springframework:spring-core ) unless using Spring Boot |
4+ or 5+, matching version |
Add any required dependencies to your Maven (pom.xml) or Gradle (build.gradle) config file, as below:
JUnit: https://mvnrepository.com/artifact/junit/junit
TestNG: https://mvnrepository.com/artifact/org.testng/testng
3. Installing JCover
Select the relevant installation options from the selection below:
3.1 Installing JCover on Linux from a zip file
Unzip the file into its own folder installation_location
.
Edit the ~/.profile
file and add the installation_location
to your search path, e.g.
PATH=$PATH:installation_location
Then, reload the profile file to make your changes active:
$ source ~/.profile
3.2 Installing JCover on macOS from a zip file
Unzip the file into its own folder installation_location
.
Edit the ~/.profile
file and add the installation_location
to your search path, e.g.
PATH=$PATH:installation_location
Then, reload the profile file to make your changes active:
$ source ~/.profile
3.3 Installing JCover on Windows 10 from a zip file
Unzip the file into its own folder installation_location
.
Access the environment variables:
Control Panel -> System and Security -> System -> Advanced System Settings -> Environment Variables
If JAVA_HOME and JCOVER_HOME do not already exist, set them up as follows:
JAVA_HOME
as the location of your JDK
JCOVER_HOME
as the installation_location
Finally, add %JCOVER_HOME%
to your PATH environment variable.
3.4 Installing JCover on Debian-based Linux (e.g. Ubuntu)
Run the following commands in a terminal window, replacing “version” with the actual version number:
$ sudo apt-get update
$ sudo apt install ./jcover_version.deb
3.5 Installing JCover on Fedora-based Linux
Run either of the following commands in a terminal window, replacing “version” with the actual version number:
$ sudo yum install ./jcover-version.rpm
or
$ sudo dnf -y install ./jcover-version.rpm
4. Check you can run JCover
To check your installation is complete, enter the following command in a terminal window:
$ jcover --version
(This also returns the version number.)
For usage and option information, enter:
$ jcover --help
5. Writing unit tests with JCover using Maven
5.1 Starting to write unit tests
A sample Spring Boot project, called sprint-petclinic, has been created so you can easily start writing unit tests with JCover.
To clone the Spring-Petclinic
project:
git clone https://github.com/spring-projects/spring-petclinic
$ cd spring-petclinic
To build the project, use either:
$./mvnw install
(Linux/macOS)
> mvnw install
(Windows)
To create tests for the project:
jcover --test-verification --spring-format --class-suffix DiffblueTest
5.2 Incremental mode
When writing unit tests for a whole module for the first time, JCover will attempt to create tests for all methods in all classes.
On any subsequent similar run, JCover will only write tests for those classes which have changed. This incremental mode
reduces analysis time dramatically and allows the user to create tests for those classes which have changed much more quickly than doing a complete analysis.
JCover will aim to rewrite tests for all classes in scope when:
- Those classes have been specified as an argument on the command line e.g.
jcover com.example.Classname
. - Classes have been modified (tests will also be produced for classes which are dependent on modified classes).
- Certain options are used which would be expected to affect the contents of test classes e.g.
--junit
/--testng
. - Modifications have been made to
pom.xml
(Maven) orbuild.gradle
(Gradle) which would be expected to affect the contents of test classes. - The classpath has changed.
- A major (0.x) version of the product is released.
6. Next steps
Please checkout the knowledge base for more guides and further information.