Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of

Fleshing Out the Test

We actually introduced an empty catch block into our system—in the test itself! Hmm. This is about the only place that an empty catch block is acceptable, but even then, there’s usually some test code we want in that catch block.

An exception throw is effectively a jump or "goto" statement. When executing a jump, it’s possible to leave things in a bad or unknown state. In the test, we need to demonstrate what the Deck object looks like after the exception gets thrown (see Listing 6).

Listing 6 Verifying Deck state after throwing the exception.

public void testDealTooMany() {
  assertDealAll(deck1, new ArrayList<Card>());
  try {
   deck1.deal();
   fail("should have received exception");
  }
  catch (EmptyDeckException expected) {
   assertEquals(0, deck1.cardsRemaining());
  }
}
  • + Share This
  • 🔖 Save To Your Account