How to find regressions
This tutorial uses Spring PetClinic with Diffblue Cover Plugin for IntelliJ, to demonstrate finding a regression in refactoring quickly (and thus also avoid merging code containing bugs into the code base).
Please start with the following steps:
git clone https://github.com/Diffblue-benchmarks/Spring-petclinic
cd Spring-petclinic
git checkout a-test-refactoring
Open the project in IntelliJ.
Navigate to the
PetTypeFormatter
class.
For this tutorial, the parse method in the PetTypeFormatter
has an awkward loop that we want to refactor into streams.
To see how well these tests cover the method, run them With Coverage
. The green bars in the margin of the source code show you which lines are covered.
Now, we refactor the method to obtain:
When you run the tests now, you’ll see that there are failing tests. That’s because the refactoring contains a bug.
The error message is: type not found: Dog
. (That’s even though we use Dog
to mock the PetRepository
in the failing test and we pass Dog
to the parse method.) We’d expect this test to pass. The best thing to do is to put a breakpoint in the failing test and run it With Debugging
to step through the implementation and see what is happening.
This reveals that the filter is wrong: we compare the pet type identifier (getId
) instead of the pet type name (getName
).
Run these tests again for regression testing in order to confirm that the refactoring is now correct and the bug is fixed.
Cover Plugin helps detect unintended changes that we had accidentally introduced.
The diagram below shows our journey through this tutorial:
Last updated