Writing unit tests with Cover CLI

A quick guide to writing unit tests for the demonstration project, java-demo, using the Cover CLI.

Cloning java-demo

The java-demo application is a simple result-checker for the traditional game of "Tic Tac Toe" (or "Noughts and Crosses") which can be found at https://github.com/diffblue/java-demo

git clone https://github.com/diffblue/java-demo

Configuring the CLI

The Cover CLI has 3 files:

  • Powershell script (Windows)
  • Bash script (Linux/OSX)
  • Cover CLI jar

To be able to use the Cover CLI on multiple projects, it is common practice to create an alias or add the relevant scripts to your path.


Add the following line to a ~/.zshrc or ~/.bashrc profile.

alias dcover="~/user/scripts/cover-cli/dcover"


Set-Alias dcover ~/user/scripts/cover-cli/dcover.bat

Writing new tests

All invocations of dcover take a single subcommand called create to generate the tests.


For example:

dcover create com.diffblue.javademo.nestedobjects --test-output-dir=src/test/java -cp target/classes/

Will produce tests for all accessible functions within package com.com.diffblue.javademo.nestedobjects (but none for any classes in com.diffblue.javademo)

Additional optional arguments may also be provided:

--classpath or -cp

-cp target/classes/

Provide the classpath of the project you want to generate tests for. Multiple paths can be specified using : as separators for Linux/OSX and ; for Windows. jar files can also be added to the classpath as below:


-cp target/classes:target/java-demo-1.0.0-jar-with-dependencies.jar

If no classpath is provided, one will be inferred. For Maven projects, this will be the project build directory plus the dependencies. For other projects, including Gradle, this will be the classpath environment variable ($CLASSPATH), if it is set, or '.' (current directory) if the environment variable is not set.

--test-output-dir or -d



Provide the relative path for the directory where the generated tests should be placed.

In this scenario, we will generate tests for the entire package by executing the alias we previously created to invoke the CLI.

cd java-demo

dcover create com.diffblue.javademo --test-output-dir=src/test/java -cp target/classes/

Generated test for method: com.diffblue.javademo.TicTacToe.<init>:()V
Generated test for method: com.diffblue.javademo.TicTacToe.checkTicTacToePosition:([I)I
Generated test for method: com.diffblue.javademo.UserAccess.<init>:()V
Generated test for method: com.diffblue.javademo.UserAccess.getCurrentUser:()Ljava/lang/String;
Generated test for method: com.diffblue.javademo.UserAccess.loginUser:(Ljava/lang/String;Ljava/lang/String;)Z
Generated test for method: com.diffblue.javademo.nestedobjects.User.<init>:(Lcom/diffblue/javademo/nestedobjects/subpackage/Order;)V
Generated test for method: com.diffblue.javademo.nestedobjects.User.checkItemCost:(Lcom/diffblue/javademo/nestedobjects/subpackage/Item;)Z
Generated test for method: com.diffblue.javademo.nestedobjects.User.setOrder:(Lcom/diffblue/javademo/nestedobjects/subpackage/Order;)Lcom/diffblue/javademo/nestedobjects/User;
Generated test for method: com.diffblue.javademo.nestedobjects.User.getOrder:()Lcom/diffblue/javademo/nestedobjects/subpackage/Order;
Generated test for method: com.diffblue.javademo.nestedobjects.subpackage.Item.<init>:()V
Generated test for method: com.diffblue.javademo.nestedobjects.subpackage.Order.<init>:()V
Generated test for method: com.diffblue.javademo.nestedobjects.subpackage.Order.hasItem:()Z
Generated test for method: com.diffblue.javademo.nestedobjects.subpackage.Order.setItem:(Lcom/diffblue/javademo/nestedobjects/subpackage/Item;)Z
Generated test for method: com.diffblue.javademo.nestedobjects.subpackage.Order.getItem:()Lcom/diffblue/javademo/nestedobjects/subpackage/Item;
Generated test for method: com.diffblue.javademo.Search.<init>:()V
Generated test for method: com.diffblue.javademo.Search.contains:([II)Z
Generated test for method: com.diffblue.javademo.Search.isNeedleInHaystack:(Ljava/lang/String;)Z

The tests have now been combined into the project at the location specified by --test-output-dir=src/test/java

Taking existing test coverage into account

It is possible to request dcover to only create new unit tests for code which is not already covered by existing tests. To do this, use the following option to pass in a path to a JaCoCo coverage report:

--existing-coverage or -e



where "target/jacoco.exec" represents the path to a JaCoCo code coverage report within the project.

For more information on the JaCoCo Java Code Coverage tool and how to use it to generate a code coverage report, please follow the Documentation link at:


results matching ""

    No results matching ""