Getting Started with the Diffblue Cover plugin for IntelliJ

CommunityTeamsEnterprise

See here for the full prerequisites for Cover IntelliJ Plugin.

1a. Installing Diffblue Cover IntelliJ Plugin directly from the IntelliJ IDE

In the IntelliJ IDE, open the Plugins menu IntelliJ IDEA -> Preferences -> Plugins (macOS) or File -> Settings -> Plugins (Windows/Linux)

Ensure the Marketplace tab is selected and search for Diffblue

Click Install

When prompted to do so, click to Restart IDE, and then click OK.

1b. Installing Diffblue Cover IntelliJ Plugin using .zip archive

Downloaded the Diffblue Cover IntelliJ Plugin as a .zip bundle from:

In the IntelliJ IDE, open the Plugins menu IntelliJ IDEA -> Preferences -> Plugins (macOS) or File -> Settings -> Plugins (Windows/Linux).

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, and then click Open. Click OK.

When prompted to do so, click to Restart IDE, and then click OK.

2. Activate a license

Following successful IDE restart you will be presented with a license activation dialogue.

Diffblue Cover requires a remote license check with the Diffblue licensing server each time it is used. Offline license activation is available with the Enterprise Edition offline option only.

For help troubleshooting network connection and proxy server settings check out our online licensing guide.

Your current license status is shown in Diffblue -> View License information.

2a. Activate Community Edition online

Community

To use Diffblue Cover Community Edition click Use Community Edition.

2b. Activate license online using license key only

TeamsEnterprise (Online)

Enter the license key XXXX-XXXX-XXXX-XXXX provided in your welcome email or provided by your organization and click Activate.

2c. Activate license offline using license key and offline license file

Enterprise (Offline)

If your organization has provided you with a license key and an offline license activation file, please follow the Enterprise Edition Offline Licensing instructions to activate.

3. Icons

Indexing - we are discovering your project structure! Meanwhile, click on the icon to write tests.

Write tests - click on the icon to write tests for this method or class.

Untestable - this method or class cannot be tested; click on the icon to find out why.

Private method - this method cannot be tested as it is private but it may be tested indirectly via a public method. Please make this method public or package protected if you would like to write unit tests.

Test writing in progress - please wait while we finish!

4. Clone the example project Spring Petclinic

Let’s look at an example project, called the Spring Petclinic project, to show Diffblue Cover plugin for IntelliJ at work.

Option 1: use git cli to clone the example project: 1. Enter the following commands:

  • git clone https://github.com/diffblue/demo-spring-petclinic
    
  • cd demo-spring-petclinic
    

2. Open this project in IntelliJ.

Option 2: use IntelliJ to clone the example project: From the IntelliJ welcome screen choose Get from VCS or from the IntelliJ IDE choose File -> New... -> Project from version control

  • Ensure “Git” is selected from the “Version control” drop down
  • Enter https://github.com/diffblue/demo-spring-petclinic as the URL
  • Press “Clone”

5. Write tests for the example project

1. Navigate to a class, for example OwnerController

2. To create tests for this class, click on the test flask icon to the left of the line public class OwnerController.

You can click the link in the Diffblue panel to see the tests produced.

package org.springframework.samples.petclinic.owner;

import static org.mockito.Mockito.any;
...

@ContextConfiguration(classes = {OwnerController.class})
@ExtendWith(SpringExtension.class)
class OwnerControllerTest {
	@Autowired
	private OwnerController ownerController;

	@MockBean
	private OwnerRepository ownerRepository;

	@MockBean
	private VisitRepository visitRepository;

	/**
	 * Method under test: {@link OwnerController#initCreationForm(Map)}
	 */
	@Test
	void testInitCreationForm() throws Exception {
		MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.get("/owners/new");
		MockMvcBuilders.standaloneSetup(ownerController)
			.build()
			.perform(requestBuilder)
			.andExpect(MockMvcResultMatchers.status().isOk())
			.andExpect(MockMvcResultMatchers.model().size(1))
			.andExpect(MockMvcResultMatchers.model().attributeExists("owner"))
			.andExpect(MockMvcResultMatchers.view().name("owners/createOrUpdateOwnerForm"))
			.andExpect(MockMvcResultMatchers.forwardedUrl("owners/createOrUpdateOwnerForm"));
	}
....

6. What does the plugin do?

The figure below shows the pipeline performed by Diffblue Cover.

  1. It first ensures that your code is compiled. This is required because it performs an analysis on the bytecode.
  2. Then it tries to trigger an execution of paths through each method that it is going to test. It uses inputs that are similar to those a developer would choose. On the way, it mocks dependencies that should be mocked.
  3. Finally, it figures out what useful assertions may be and adds them to the test. Note that the assertions always reflect the current behaviour of the code.

7. Next steps

Diffblue Cover is intended to be used for unit regression testing. That means, it creates tests that reflect the current behaviour of your code. Later when you make changes to the code, these tests may fail and show you possibly unintended changes to the code, so-called regressions.

Diffblue Cover also helps you speed up unit test writing for new features because it produces tests that would take you a long time to write from scratch. A third application is to find bugs by review.

Additionally Diffblue Cover can help developers get started writing tests manually by providing skeleton tests for a given class or method. This can be particularly useful when a known bug needs to be reproduced as a test case in preparation for a fix to be developed.

Please checkout our documentation for more guides, videos and information on using the Diffblue Cover plugin for IntelliJ.