From 4ae79849aaeada600eb46d5405f35f527423ecd4 Mon Sep 17 00:00:00 2001 From: Christopher Bohn <bohn@unl.edu> Date: Thu, 27 Jun 2019 07:04:04 -0500 Subject: [PATCH] added testPopFromEmptyStack() --- src/test/java/MyStackTest.java | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/test/java/MyStackTest.java b/src/test/java/MyStackTest.java index 38de92b..2f150db 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 -- GitLab