Knowledge Base > IntelliJ Plugin > Creating incomplete tests

Creating incomplete tests

Incomplete tests

Normally, selecting  or Write Tests will create complete tests for a method. A complete test consists of Arrange, Act, and Assert sections and passes when executed. However, sometimes Diffblue Cover is not (yet) able to produce complete tests and will give the reason for not producing complete tests in the Diffblue Cover tool window. There are options to output incomplete tests in these cases. Such tests may be incomplete in various aspects:

  • The test does not have assertions.
  • The test does not always pass when executed.
  • The Arrange or Act sections produce an error when executed.
  • The test may execute code that is potentially harmful to your system, leaks resources or times out. Outputting such tests is useful to developers for two reasons:
  • Firstly, it will save developers time over writing tests from scratch because they might be able to easily turn such an incomplete test into a complete one.
  • Secondly, it may help developers understand why Diffblue Cover was not able to complete the test automatically in cases where the reason for not producing a test is lack of testability, for instance.

For example, for method increment in the following class:

public class MyClass {
    private int x;
    public void increment() {
	  ++x;
    }
}

Diffblue Cover will produce an incomplete test

  @Test
  public void testIncrement() {
    // TODO: This test is incomplete.
    //   Reason: R002 Missing observers.
    //   Diffblue Cover was unable to create an assertion.
    //   Add getters for the following fields or make them package-private:
    //     MyClass.x

    // Arrange
    MyClass m = new MyClass();

    // Act
   m.increment();
  }

The problem is clearly that Diffblue Cover has no opportunity to assert on the side effect of the increment method on field x, which is marked private.. The developer could add a getter to MyClass:

public class MyClass {
    private int x;
    public void increment() {
	  ++x;
    }
	public int getX() {
	  return x;
	}
}

Invoking Write Tests again would create a complete test now.

Enabling the creation of incomplete 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 in the Test Generation section, check the boxes for the desired Allow writing tests ... options.