diff --git a/src/test/java/MyStackTest.java b/src/test/java/MyStackTest.java index 38de92bc88fbc3dd2f1250b9d59d7677d0483261..2f150db8af9046e115e7c2845fac18e80287fd9d 100644 --- a/src/test/java/MyStackTest.java +++ b/src/test/java/MyStackTest.java @@ -1,6 +1,10 @@ import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; + +import java.util.EmptyStackException; import static org.junit.Assert.*; @@ -65,4 +69,35 @@ public class MyStackTest { stack.pop(); assertEquals(2,stack.size()); } + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + @Test + public void testPopFromEmptyStack() { + thrown.expect(EmptyStackException.class); + // EmptyStackExpression says it all, but if the were a constructor that took a String then we could test + // thrown.expectMessage("Attempted to pop from empty stack."); + stack.pop(); + } + +/* WILL TEST IF *ANY* CODE IN TEST METHOD THROWS EXCEPTION -- OKAY FOR SMALL TEST METHODS + @Test(expected = EmptyStackException.class) + public void testPopFromEmptyStack() { + stack.pop(); + } +*/ + +/* JUNIT 3 IDIOM + @Test + public void testPopFromEmptyStack() { + try { + stack.pop(); + fail("Expected EmptyStackException to be thrown."); + } catch (EmptyStackException emptyStackException) { + // EmptyStackExpression says it all, but if the were a constructor that took a String then we could test + // assertThat(emptyStackExpression.getMessage(), is("Attempted to pop from empty stack.")); + } + } +*/ } \ No newline at end of file