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/...

Writing new tests

All invocations of dcover take a single subcommand called create and two required options to generate the tests.


For example:

dcover create com.diffblue.javademo.nestedobjects --test-sources-root 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)

The additional required arguments are:

--classpath or -cp

-cp target/classes/

Provide the classpath of the project you want to generate tests for.


--test-sources-root src/test/java

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-sources-root 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-sources-root src/test/java

results matching ""

    No results matching ""