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. Cover automatically maintains the tests by updating them when your code changes. Cover supports standard Java 8 & 11, Spring and Spring Boot.

Cover 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 Cover plug-in for IntelliJ IDEA for interactive test writing.

Increasing coverage with Diffblue Cover CLI

dcover generating tests

  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 Cover 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

Using Cover IntelliJ plug-in to write tests

dcover plugin generating tests

  1. Install Diffblue Cover Intellij plug-in
  2. Start IntelliJ and load project
  3. Write code
  4. Right-click on class or method and select Write Tests

An example using Spring Petclinic

Cover 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, Cover 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 Diffblue Cover

@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 Diffblue Cover generated tests can be found in the examples topic

results matching ""

    No results matching ""