An in-depth tutorial

With easyb it's quite easy to create stories (which are logically made up of scenarios) that facilitate driving development, much like TDD is supposed to do. That is, easyb (and BDD concepts) can seriously assist your test-first-development desires; however, you don't need to focus on tests-- just focus on the intended behavior! It happens to be easy too. Remember, easyb may be a Groovy framework but it is for Java-- so if you are coding normal Java applications, you can use easyb!

Using roughly the same (albeit prosaic, but oh so easy) Stack example from IBM developerWorks' Adventures in behavior-driven development, easyb can be used to fashion a series of scenarios that describe a stack data-structure before you actually write it-- because easyb stories are Groovy, you don't have to worry about compilation issues either. Even better, easyb stories are single Groovy files that have scenarios in sequential order.

Pushing null

Thinking at a high level about a stack, one could maybe specify that:

given an empty stack, when null is pushed, an exception should be thrown and the stack should still be empty.

Whether or not you actually create a place holder object first is up to you; however, for the purposes of this example, the first step is to create a Stack object (in Java, of course!).


public class Stack<E> {
 public Stack() {
 }
}

The scenario can then be written out as follows:


given "an empty stack",{
 stack = new Stack()
}

when "null is pushed", {
 pushnull = {
  stack.push(null)
 }
}

then "an exception should be thrown", {
 ensureThrows(RuntimeException){
  pushnull()
 }
}

and "then the stack should still be empty", {
 stack.empty.shouldBe true
}	

Note how multiple thens were used; plus, see the when clause? A closure is created to essentially capture the intended behavior, which will be exercised in the first then. As you can infer, the ensureThrows method take two parameters-- an exception type and a closure to invoke. Logically, we expect that the call to push with null will yield a RuntimeException. The second then clause then ensures that the stack is still empty.

Of course, executing this story will yield some errors-- as you can see below, the isEmpty method isn't defined yet! What about the push call as well? It turns out that an exception was generated as a result of that invocation; however, the framework caught it (because it was looking for a RuntimeException, which apparently groovy.lang.MissingMethodException is a subclass of).


1) Specification the stack should still be empty:
 groovy.lang.MissingMethodException: No signature of method: \
 io.easyb.bdd.stack.Stack.isEmpty() is applicable for \
   argument types: () values: {}
  at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:572)
  at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:450)

Accordingly, the stack needs an isEmpty method defined.


public boolean isEmpty(){
 return true;
}

The stack still needs a push method too.


public void push(E value){
 throw new RuntimeException();
}

Running this story now works out nicely.

Empty pops

The stack isn't finished yet-- in fact, as coded, it only works to make a story succeed. BDD, however, is an excellent mechanism for collaboration between stakeholders and development; as such, it makes sense to delay writing the actual implementation as long as possible and instead focus on behavior.

Therefore, the next behavior of a stack could be:

given an empty stack, when pop is called an exception should be thrown and the stack should still be empty

Coding this scenario in easyb is easy too-- just add it to the existing story! It looks quite familiar, right?


given "an empty stack",{
 stack = new Stack()
}

when "pop is called", {
 popnull = {
  stack.pop()
 }
}

then "an exception should be thrown", {
 ensureThrows(RuntimeException){
  popnull()
 }
}

and "then the stack should still be empty", {
 stack.empty.shouldBe true
}

Running the story now yields another issue-- the pop method isn't defined.


public E pop(){
 throw new RuntimeException();
}

Now things work and the story has two scenarios.

Push and pop working together

Moving on, another behavior relates to the logical behavior of push and pop working together:

given a stack with one pushed value, when pop is called that object should be returned and the stack should be empty.

Things are looking easy, eh?


given "an empty stack with one pushed value",{
 stack = new Stack()
 pushVal = "foo"
 stack.push(pushVal)
}

when "pop is called", {
 popVal = stack.pop()
}

then "that object should be returned", {
 popVal.shouldBe pushVal
}

and "then the stack should be empty", {
 stack.empty.shouldBe true
}

In the above code, a value is pushed and then popped and the values are compared for equality. Things don't work, however. The push method is defined to throw an exception so the scenario fails.


....There was an error running the script
java.lang.RuntimeException
 at io.easyb.bdd.stack.Stack.push(Stack.java:10)
 at gjdk.org.bdd.story.stack.Stack_GroovyReflector.invoke(Unknown Source)

Time to code a real pop method! Oh yeah, and a push one as well.


public class Stack<E> {
 private ArrayList<E> list;

 public Stack() {
  this.list = new ArrayList<E>();
 }

 public void push(E value) {
  if (value == null) {
   throw new RuntimeException("Can't push null");
  } else {
   this.list.add(value);
  }
}

 public boolean isEmpty() {
  return true;
 }

 public E pop() {
  if (this.list.size() > 0) {
   return this.list.remove(this.list.size() - 1);
  } else {
   throw new RuntimeException("Nothing to pop");
  }
 }
}

Now the story is starting to work again!


6 behavior steps run successfully  

Coding isEmpty

It seems the isEmpty method is still hardcoded, which leads to another behavior:

given a stack with any pushed values, isEmpty should return false.

This scenario is even easier to code with easyb-- in fact, you can forget the when clause all together (if you want).


given "an empty stack with one pushed value",{
 stack = new Stack()
 stack.push("bar")
}

then "the stack should not be empty", {
 stack.empty.shouldBe false
}

The empty test fails of course.


......F
Time: 0.92s

Failures: 1.

1) Specification the stack should be empty:
 VerificationException: Expected: 
 equal to [false]
 but got: 
 true:

Coding it in Java is easy.


public boolean isEmpty() {
 return this.list.isEmpty();
}

The meat of a stack

The meat of a stack is the behavior of push and pop called multiple times; accordingly

given a stack with two values, when pop is called the last item pushed should be returned.


given "a stack with two values",{
 stack = new Stack()
 push1 = "foo"
 push2 = "bar"
 stack.push(push1)
 stack.push(push2)
}

when "pop is called", {
 popVal = stack.pop()
}

then "the last item pushed should be returned", {
 popVal.shouldBe push2
}

and "then the stack should not be empty", {
 stack.empty.shouldBe false
}

Things read nicely don't they? Don't forget, this scenario is in one file containing the entire story.

Peek's behavior

The peek method is commonly found on stacks, as such:

given a stack containing items, when peek is called, it should provide the value of the most recent pushed value; however, that value should remain in the stack.


given "a stack containing an item",{
 stack = new Stack()
 push1 = "foo"
 stack.push(push1)
}

when "peek is called", {
 peeked = stack.peek()
}

then "it should provide the value of the most recent pushed value", {
 peeked.shouldEqual "foo"
}

and "then the stack should not be empty", {
 stack.empty.shouldBe false
}

and "then calling pop should also return the peeked value", {
 	def popped = stack.pop()
    popped.shouldBe(push1)
    and
    popped.shouldBe(peeked)
}

and "then the stack should be empty", {
 stack.empty.shouldBe true
}

This scenario is fairly detailed and long; however, the point is valid-- you can use as many thens as you need.

Oh yeah, peek wasn't ever coded.


.........There was an error running the script
groovy.lang.MissingMethodException: No signature of method: 
 io.easyb.bdd.stack.Stack.peek() is applicable for
  argument types: () values: {}
 at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:572)

Coding peek takes a New York second.


public E peek() {
 if(this.list.size() > 0){
  return this.list.get(this.list.size()-1);
 }else{
  return null;
 }
}

Time to run the entire story!


13 behavior steps run successfully  

In this tutorial, a stack was created by driving development with BDD. In essence, easyb was used to tell the story of a stack-- as things failed, the proper behavior was coded. Scenarios were written in literate form via easyb's constructs and they all aggregated up to create a story that lives as executable documentation for the object being verified.

Wow, aren't things easy with easyb?