I'm writing a Stack class using the Test Driven Design concept.
In the setUp() method my stack is created with 0 elements like this
Stack stack = new Stack();
This is my attempted test to catch the StackEmptyException which would be raised when top is immediately called after setUp().
@Test
public final void testTopIsEmpty() throws StackEmptyException
{
StackEmptyException thrown = null;
try
{
stack.top();
}
catch (StackEmptyException caught)
{
thrown = caught;
}
assertThat(thrown, is(instanceOf(StackEmptyException.class)));
}
My problem is in the last line. I don't understand why my code doesn't work!
The correct way to test for an exception in JUnit is: