import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; /** * JUnit test cases for class GameDie */ public class GameDieTest { // data fields private GameDie dieDefault; private GameDie dieTen; /** * This @Before method is run before EVERY * @Test method, creating an example die using * each constructor */ @Before public void setUp() throws Exception { dieDefault = new GameDie(); dieTen = new GameDie(10); } /** * test the getTop method */ @Test public void testGetTop() { assertEquals("new default die's top should be 1", 1, dieDefault.getTop()); assertEquals("new 10-sided die's top should be 1", 1, dieTen.getTop()); int exRoll = dieDefault.roll(); assertEquals("rolled die's top should equal its roll", exRoll, dieDefault.getTop()); exRoll = dieTen.roll(); assertEquals("rolled die's top should equal its roll", exRoll, dieTen.getTop()); } /** * test the roll method */ @Test public void testRoll() { int testRollResult; // rolling a default die, the values should ALL // be in [1, DEFAULT_SIDES] for (int i=0; i<10000; i++) { testRollResult = dieDefault.roll(); assertTrue("roll must be <= 6, and was: " + testRollResult, testRollResult <= 6); assertTrue("roll must be >= 1", testRollResult >= 1); assertEquals("roll should set top to rolled value", testRollResult, dieDefault.getTop()); } // rolling a 10-sided die, the values should ALL // be in [1, 10] for (int i=0; i<10000; i++) { testRollResult = dieTen.roll(); assertTrue("roll must be <= 10, and was: " + testRollResult, testRollResult <= 10); assertTrue("roll must be >= 1", testRollResult >= 1); assertEquals("roll should set top to rolled value", testRollResult, dieTen.getTop()); } } }