L'etat, c'est moi
Mere Complexities sells the consulting and development services of me, Paul Wilson.
Conferences
Archive
Testing class invariants
One way of looking at unit tests is in terms of Design by Contract: most tests define postconditions and there can also be value in having tests showing that a class fails-fast on precondition failure. While helping out on a TDD course this week it occurred to me that Junit 4 provides a simple mechanism for checking class invariants in unit tests: it’s particularly useful to be confident invariants hold even after exceptions have been thrown.
For example, a game of Blackjack may allow dealing only once.
@After
public void ensureInvariants()
{
assertTrue(game.canDeal() == !game.isDealt());
}
Of course, it wouldn’t be rocket science to do the same in Junit 3.8:
<pre>
(PS credit to Malcolm Sparks for suggesting that I blog this.)