Getting started with the Diffblue Cover IntelliJ plugin

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



1. Download Cover IntelliJ plugin

Please contact Diffblue to obtain the Cover IntelliJ plugin

2. Prerequisites

  • IntelliJ version 2019.3
  • Java 8 compatible source code
  • Maven or Gradle-based project

Dependencies required for running tests should be in the project configuration. JUnit is always mandatory but other libraries may also be necessary depending on the project under test.

Dependency When Version
JUnit (junit:junit or org.junit.jupiter:junit-jupiter-engine) Always 4.7-5.6
Spring Boot Test (org.springframework:spring-boot-test) when using Spring Boot (org.springframework:spring-boot) 2+, matching versions
Spring Test (org.springframework:spring-test) when using Spring (org.springframework:spring-core) unless using Spring Boot 5+, matching version

3. Installing Cover IntelliJ Plugin

Download the zip file and unzip in a suitable location

From the IntelliJ IntelliJ IDEA -> Preferences (macOS) or File -> Settings (Windows/Linux) menu, select the Plugins item.

Click on the cog-wheel icon to the right of the Installed tab at the top of the window. From the drop-down menu, select Install Plugin from Disk...

Navigate to the location of the plugin, select the Zip file, click OK and restart the IDE.

4. Importing CoreBanking

The CoreBanking application is a simple banking application for demo purpose which can be found at https://github.com/diffblue/CoreBanking.

Start IntelliJ without a project loaded to see a welcome screen:

Choose Git from the Check out from Version Control option. Enter the CoreBanking url into the repository box, selecting an appropriate location for the project.

Once it has been successfully cloned, expand the CoreBanking section under Project to see the source tree.

5. Writing new tests

In order to generate new tests for a class which is not currently covered, select the Account class in the Project tool window. Right-click* and select the Write Tests option from the menu.

The plugin builds the project before analyzing the code and creating the tests. Once the project has been built, click the progress bar at the bottom of the screen to open a pop-up window.

The progress window will show the different stages that the analysis is going through. While the code is being analyzed, it is possible to view current progress, but with CoreBanking that will be a very short time-frame.

Once the new tests have been created, they will be automatically integrated into the project under src/test/java.

* Note that it is also possible to Write Tests by right-clicking the following:

  • individual classes from within the source editor
  • individual methods from within the source editor
  • individual classes from the tab at the top of the source file
  • individual methods from within their classes in the Project menu (if Show Members has been selected after right-clicking on the Project menu)

Elsewhere within the IDE, for example in test classes or non-Java files, the Write Tests feature is disabled.

6. Running the tests

After Diffblue Cover has written tests for your project, they will be automatically integrated into your test suite. Tests created by Diffblue Cover make use of the JUnit testing framework so junit is included in the CoreBanking project dependencies in the pom.xml.

Select the new AccountTest class in the project explorer, right-click and Run 'AccountTest'.

This will then show the results of the JUnit run:

7. Automatically suggesting assertions

As well as writing tests, Cover can suggest assertions for JUnit tests. In this example we have started writing a test for the Client.takeFromBalance() method:

Source

package com.diffblue.corebanking.account;

import com.diffblue.corebanking.client.Client;
import org.junit.Assert;
import org.junit.Test;


public class AccountTest {
  @Test
  public void testTakeFromBalance() throws Exception {
    // Arrange
    Client client = new Client("John Smith");
    Account account = new Account(19928294, client, 10000);

    // Act
    account.takeFromBalance(250);

    // Assert

  }
}

We have setup the call to the MUT, but do not yet have any assertions. Cover can automatically write the assertions by placing the cursor where we want them inserted and hit Alt+Shift+Space. After completing the analysis, the following list is displayed:

We can choose an assertion to insert (or several by using Ctrl+Click), and add it to our test. Let’s insert the one that checks the current balance. Our assertion section now looks like this:

// Assert
assertEquals(9750L, account.getCurrentBalance());

Some cases where Diffblue Cover may not be able to provide any assertion suggestions:

  • Usage of another framework than JUnit4
  • Test methods containing mocking
  • Test methods that do not already successfully run.

8. Next steps

Please checkout the knowledge base for more guides, videos and information

results matching ""

    No results matching ""