Covering all enum values

Cover Plugin for IntelliJ can write tests which ensure that all possible values of an Enum type are covered by the test suite, when that Enum is passed to a method as an argument or is a return value of a method.

With and without "Cover all enum values"

To understand the difference that the Cover all enum values feature makes to created tests, we can write tests on a simple class which takes an Enum type as an argument and behaves in different ways depending on that argument's value.

public class ErrorFormatter {

  private static final String NON_FATAL_ERROR_TEMPLATE =
      "<p style=\"color:orange\"><em>non-fatal: %s</em></p>";

  private static final String FATAL_ERROR_TEMPLATE =
      "<p style=\"color:red\"><strong>FATAL! %s</strong></p>";

  public String asHtml(ErrorLevel level, String message) {
    switch (level) {
      case INFORMATIONAL:
      case WARNING:
        return String.format(NON_FATAL_ERROR_TEMPLATE, message);
      case FATAL:
        return String.format(FATAL_ERROR_TEMPLATE, message);
      default:
        throw new AssertionError(String.format("Unrecognised error level %s", level));
    }
  }
}

Note in particular that the method exhibits the same behavior when the level argument is INFORMATIONAL or WARNING. Two tests that exercise this method by passing those two values to its first argument will therefore cover the same lines of code.

When we write tests on this method with Cover all enum values disabled, then Cover Plugin creates these tests:

@Test
public void testAsHtml() {
  assertEquals("<p style=\"color:orange\"><em>non-fatal: Not all who wander are lost</em></p>",
      (new ErrorFormatter()).asHtml(ErrorLevel.INFORMATIONAL, "Not all who wander are lost"));
  assertEquals("<p style=\"color:red\"><strong>FATAL! Not all who wander are lost</strong></p>",
      (new ErrorFormatter()).asHtml(ErrorLevel.FATAL, "Not all who wander are lost"));
}

When we enable Cover all enum values and write tests again, Cover Plugin creates these tests:

@Test
public void testAsHtml() {
  assertEquals("<p style=\"color:orange\"><em>non-fatal: Not all who wander are lost</em></p>",
      (new ErrorFormatter()).asHtml(ErrorLevel.INFORMATIONAL, "Not all who wander are lost"));
  assertEquals("<p style=\"color:orange\"><em>non-fatal: Not all who wander are lost</em></p>",
      (new ErrorFormatter()).asHtml(ErrorLevel.WARNING, "Not all who wander are lost"));
  assertEquals("<p style=\"color:red\"><strong>FATAL! Not all who wander are lost</strong></p>",
      (new ErrorFormatter()).asHtml(ErrorLevel.FATAL, "Not all who wander are lost"));
}

With Cover all enum values enabled the Cover Plugin creates an extra test, to ensure that both the INFORMATION and WARNING levels are tested, even though the extra test may not increase the test suite's measured coverage.

Disable "Cover all enum values"

The Cover all enum values feature is enabled by default. To disable, go to Diffblue > Change Settings > Test Creation in IntelliJ, and uncheck the Allow writing tests for all enum values option.

Last updated