Tópicos sobre escrita de testes com JUnit/Random bombs

From Wiki**3

< Tópicos sobre escrita de testes com JUnit

The interface:

/**
 * Random bombs are unreliable devices.
 */
interface RandomBomb {

 /**
  * Detonate the bomb (make it explode).
  * @throws NoExplosion when the bomb fails to explode.
  */
 void detonate() throws NoExplosion;

 /**
  * Disarm the bomb.
  * @throws Explosion when the bomb explodes.
  */
 void disarm() throws Explosion;

}

An implementation:

public class UnifDistBomb implements RandomBomb {

 /**
  * @see RandomBom#detonate
  */
 public void detonate() throws NoExplosion {
   if ((int)(Math.random()*100)%2 == 0) throw new NoExplosion();
 }

 /**
  * @see RandomBom#disarm
  */
 public void disarm() throws Explosion {
   if ((int)(Math.random()*100)%2 == 0) throw new Explosion();
 }

}

The tests:

import junit.framework.TestCase;

public class RandomBombTest extends TestCase {

 private RandomBomb _bomb;

 public void setUp() {
   // testExplosion and testNoExplosion get different bombs!!
   _bomb = new UnifDistBomb();
 }

 public void testExplosion() {
   try {
     _bomb.detonate();
   }
   catch (NoExplosion e) {
     fail("did not explode");
   }
 }

 public void testNoExplosion() {
   try {
     _bomb.disarm();
   }
   catch (Explosion e) {
     fail("exploded!");
   }
 }

}

Trial runs (all possible cases):

java -cp .:junit.jar junit.textui.TestRunner RandomBombTest
..
Time: 0.034

OK (2 tests)
java -cp .:junit.jar junit.textui.TestRunner RandomBombTest
.F.
Time: 0.002
There was 1 failure:
1) testExplosion(RandomBombTest)junit.framework.AssertionFailedError: did not explode
        at RandomBombTest.testExplosion(RandomBombTest.java:18)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

FAILURES!!!
Tests run: 2,  Failures: 1,  Errors: 0
java -cp .:junit.jar junit.textui.TestRunner RandomBombTest
.F.F
Time: 0.002
There were 2 failures:
1) testExplosion(RandomBombTest)junit.framework.AssertionFailedError: did not explode
        at RandomBombTest.testExplosion(RandomBombTest.java:18)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
2) testNoExplosion(RandomBombTest)junit.framework.AssertionFailedError: exploded!
        at RandomBombTest.testNoExplosion(RandomBombTest.java:27)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

FAILURES!!!
Tests run: 2,  Failures: 2,  Errors: 0
java -cp .:junit.jar junit.textui.TestRunner RandomBombTest
..F
Time: 0.002
There was 1 failure:
1) testNoExplosion(RandomBombTest)junit.framework.AssertionFailedError: exploded!
        at RandomBombTest.testNoExplosion(RandomBombTest.java:27)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

FAILURES!!!
Tests run: 2,  Failures: 1,  Errors: 0