
Investing time in improving your reds will prove very useful for your colleagues and yourself because the clearer the error message, the better the context to fix the regression error effectively. Most of the times, applying this small variation to the TDD cycle only requires a small effort. As a simple example, have a look at the following assertion and how it fails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
assertThat(new Fraction(1,2).add(1), is(new Fraction(3,2))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
java.lang.AssertionError: | |
Expected: is <Fraction@2d8e8c3a> | |
but: was <Fraction@2d8e84b8> |
The answer is clearly no, but with little effort, we can add a bit of code to make the error message clearer (implementing the
toString()
method).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public String toString() { | |
return "Fraction{" + | |
"numerator=" + numerator + | |
", denominator=" + denominator + | |
'}'; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
java.lang.AssertionError: | |
Expected: is <Fraction{numerator=3, denominator=2}> | |
but: was <Fraction{numerator=1, denominator=2}> |
toString()
generated by our IDE.
Take that low hanging fruit, start improving your reds!
Originally published in Codesai's blog.
No comments:
Post a Comment