Creating boilerplate tests

Normally, selecting Write Tests will generate one test per method. If the Diffblue Cover IntelliJ plugin is unable to create a usable test for a method, it can use the "boilerplate" mode, if enabled (see below), where it will produce a template for a test based on values relevant to the source code under test. This will save test developers time over writing tests from scratch.

For example, the method in the following inner class in an outer class, DatabaseDao:

public static class Inner {
    private Inner(){};
    public void myMethod(Inner inner) {
    }
}

could produce a boilerplate test like this:

  @Test
  public void testInnerMyMethod() throws Exception {
    // Arrange
    String string = "This is a boilerplate test. Please fix.";
    Assert.fail(string);
    DatabaseDao.Inner inner = null;
    DatabaseDao.Inner inner1 = null;

    // Act
    inner.myMethod(inner1);
  }

Note that boilerplate tests will not be generated for methods where there is no value to be gained by testing. For example, a method may be a trivial setter or getter, may be a main method, may be a private method or an instance method of private class. In such situations the Diffblue Cover IntelliJ plugin will skip test generation and will output the testability status of the given method to the event log.

Enabling the creation of boilerplate tests

In order to switch this feature on, under File -> Settings (Windows/Linux) or IntelliJ IDEA -> Preferences (macOS), select Tools -> Diffblue Cover, then select the Generation tab of the Diffblue Cover configuration screen, and check the box for Generate boilerplate tests at the top of this window. Click OK.

results matching ""

    No results matching ""