Covering all enum values
The Diffblue Cover Plugin for IntelliJ can write tests which ensure that all values of an enum are covered when an enum is passed as an argument or returned from a method.
The Diffblue 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.To understand the difference
Cover all enum values
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 upon 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 Diffblue Cover Plugin for IntelliJ produces 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, the Diffblue Cover Plugin for IntelliJ produces 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 Diffblue Cover Plugin for IntelliJ 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.This behavior is enabled by default. To turn off this behavior, go in IntelliJ IDEA to
File -> Settings
(Windows/Linux) or IntelliJ IDEA -> Preferences
(macOS), and uncheck the Diffblue Cover -> Test Naming -> Cover all enum values
box.Last modified 23d ago