Skip to content

Instantly share code, notes, and snippets.

@bbasata
Created November 5, 2011 02:02
Show Gist options
  • Save bbasata/1340977 to your computer and use it in GitHub Desktop.
Save bbasata/1340977 to your computer and use it in GitHub Desktop.
An example of BDD-influenced use of JUnit
import org.junit.Test;
import java.util.EmptyStackException;
import java.util.Stack;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
public class StackTest {
Stack<String> stack = new Stack<String>();
@Test public void
isInitiallyEmpty() {
assertThat(stack.isEmpty(), is(true));
}
@Test(expected=EmptyStackException.class) public void
refusesToPopWhenItIsEmpty() {
stack.pop();
}
@Test public void
isNotEmptyWhenItHasAnItem() {
stack.push("an item");
assertThat(stack.isEmpty(), is(false));
}
@Test public void
popsAnItem() {
stack.push("first item");
String poppedItem = stack.pop();
assertThat(poppedItem, is("first item"));
}
@Test public void
removesAnItemWhenItIsPopped() {
stack.push("first item");
stack.pop();
assertThat(stack.isEmpty(), is(true));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment