Basic Concepts

What is Diffblue Cover?

Diffblue Cover is an automated unit test-writing tool. It analyses your existing Java application and writes unit tests that reflect the current behavior, increasing test coverage and helping you find regressions in future code changes. DCover automatically maintains the tests by updating them when your code changes, and supports standard Java 8 & 11, Spring and Spring Boot.

DCover is available as a CLI tool for Windows and Linux and works 100% autonomously, configuring itself from your Maven or Gradle environment. There is also a Diffblue Cover plugin for IntelliJ IDEA for interactive test writing.

Increasing coverage with DCover CLI

  1. Compile the application
  2. Run tests and benchmark coverage using your favorite tool (e.g. JaCoCo)
  3. Run dcover create to generate a Diffblue test baseline
  4. Run Diffblue tests and verify coverage increase.

Using DCover CLI to find bugs

  1. Compile the application
  2. Run dcover create to generate baseline unit tests
  3. Make a change to the code
  4. Run existing Diffblue tests
  5. Fix any bugs flagged by Diffblue test failures
  6. Re-run dcover create to update tests based on new code if required.

An example using Spring PetClinic

DCover endeavors to create unit tests that are indistinguishable from human written unit tests. The main identifier that the tests have been generated automatically is the suffix DiffblueTest in test class names. In the following example from Spring PetClinic, DCover has created a new instance of the object Owner

Source

	@GetMapping("/owners")
	public String processFindForm(Owner owner, BindingResult result, Map<String, Object> model) {

		// allow parameterless GET request for /owners to return all records
		if (owner.getLastName() == null) {
			owner.setLastName(""); // empty string signifies broadest possible search
		}

		// find owners by last name
		Collection<Owner> results = this.owners.findByLastName(owner.getLastName());
		if (results.isEmpty()) {
			// no owners found
			result.rejectValue("lastName", "notFound", "not found");
			return "owners/findOwners";
		}
		else if (results.size() == 1) {
			// 1 owner found
			owner = results.iterator().next();
			return "redirect:/owners/" + owner.getId();
		}
		else {
			// multiple owners found
			model.put("selections", results);
			return "owners/ownersList";
		}
	}

Test generated by DCover:

@Test
  public void testProcessFindForm() throws Exception {
    // Arrange
    Owner owner = new Owner();
    owner.setLastName("Doe");
    owner.setCity("Oxford");
    owner.setAddress("42 Main St");
    owner.setFirstName("Jane");
    owner.setTelephone("4105551212");
    ArrayList<Owner> ownerList = new ArrayList<Owner>();
    ownerList.add(owner);
    when(this.ownerRepository.findByLastName(or(isA(String.class), isNull()))).thenReturn(ownerList);

    // Act
    ResultActions actualPerformResult = this.mockMvc.perform(MockMvcRequestBuilders.get("/owners"));

    // Assert
    ResultActions resultActions = actualPerformResult.andExpect(MockMvcResultMatchers.status().isFound());
    resultActions.andExpect(MockMvcResultMatchers.model().size(0));
  }

Further examples of DCover generated tests can be found in the examples topic. Please see the knowledge base for details of how to build your project.