I like writing good exhaustive tests in JUnit, making use of @Nested to test all possible states, but I've found that for test subjects with a lot of state transitions, it can lead to huge unwieldy tests especially when it can take many steps to get to a specific state, and at each level you should probably try every allowed transition. This makes it hard to see if all paths are truly tested, and due to the size of the test it becomes difficult to modify or extend.
So over the past year I've been using a different way of testing. Instead of writing many nested levels with all possible steps, I've been writing tests that only define what possible actions can be applied to a subject under test. These actions are then automatically combined to explore all paths, where each path terminates until it reaches a state that has been seen before (or a selectable maximum depth has been reached).
For example, let's say I wrote a custom Queue implementation based on a LinkedHashSet that disallowed duplicates. I'd define actions to perform on the queue like:
() -> queue.poll();
() -> queue.offer(value);
() -> queue.remove(value);
To verify the queue works correctly, one can compare it with say an ArrayList. Each action is then defined by first updating your expectation, and returning the action to apply to the subject under test:
@Action
@ValueSource(strings = {"A", "B", "C"})
public Runnable enqueue(String value) {
// update expectation (disallowing duplicates):
if (!expectedQueue.contains(value)) {
expectedQueue.add(value);
}
// the action on the subject:
return () -> queue.offer(value);
}
To verify the queue matches the expected queue one can define one or more assertion methods:
@Assertion
public void assertQueueContents() {
assertThat(List.copyOf(queue)) // turn queue under test into a List
.describedAs("Queue contents")
.isEqualTo(expectedQueue); // ensure it matches our expectation
}
In order for the ExploratoryTestRunner to prune paths with states that have already been reached before, the test code must implement the Explorable interface which requires the implementation of a snapshot method. This method should simply take the expected state (copying it if needed) and return an Object that can be compared with equals. Usually using a record here is optimal. For example:
public record State(List<String> queue) {}
@Override
public Object snapshot() {
return new State(List.copyOf(expectedQueue));
}
The whole class then looks roughly like this:
class LinkedHashSetAsDeduplicatingQueueExploratoryTest {
@Test
void exploreQueueSemantics() {
ExploratoryTestRunner.explore(QueueExplorable.class, QueueExplorable::new);
}
public static class QueueExplorable implements Explorable {
private final Queue<String> queue = new MyQueue<>();
private final List<String> expectedQueue = new ArrayList<>();
public record State(List<String> queue) {}
// snapshot, action and assertion methods omitted here
}
}
When run, this ExploratoryTestRunner will explore all paths defined by the test class, creating new instances of QueueExplorable as needed. It will then report how many states it tested and what the deepest path was:
ExploratoryTestRunner: class
examples.LinkedHashSetAsDeduplicatingQueueExploratoryTest$QueueExplorable -- Explored 660 paths, longest path: 5
If a failure occurs, this is reported by showing the path that leads to the failure, and which assertions failed (including a helpful trace line), for example:
org.opentest4j.AssertionFailedError: Path 61 failed:
- enqueue(A) -> State[queue=[A]]
- enqueue(B) -> State[queue=[A, B]]
- dequeue -> State[queue=[B]]
[Queue contents]
expected: ["B"]
but was: ["A"]
at org.int4.common.test/examples.LinkedHashSetAsDeduplicatingQueueExploratoryTest$QueueExplorable.assertQueueContents(LinkedHashSetAsDeduplicatingQueueExploratoryTest.java:42)
[Peeked element]
expected: "B"
but was: "A"
at org.int4.common.test/examples.LinkedHashSetAsDeduplicatingQueueExploratoryTest$QueueExplorable.assertPeekedElement(LinkedHashSetAsDeduplicatingQueueExploratoryTest.java:49)
The above example shows quite clearly that dequeue seems to have removed the wrong element (in this case because getLast was called instead of getFirst in the subject under test).
The ExploratoryTestRunner can be found here: https://github.com/int4-org/Common/tree/master/common-test
The full example test case is here: https://github.com/int4-org/Common/blob/master/common-test/examples/LinkedHashSetAsDeduplicatingQueueExploratoryTest.java
Another much more elaborate example (for a UI control): https://github.com/int4-org/FX/blob/master/fx-builders/src/test/java/org/int4/fx/builders/control/TextFieldControlExploratoryTest.java