As we discussed earlier tests shows how the program under test should work.
JUnit test can be understood by people having some Java background. But what’s about people on your team that have no programming background but still are interested in the way the program work or even want to contribute there own expectations.

Those problems are addressed by Behavior Driven Development (BDD).
There are dozen of frameworks for BDD and they all trying to make testing more readable. In my opinion BDD Tests also improves structure of the testing itself.

The framework can be divided in two categories. One that use internal DSL and others that use external DSL. (for a few months I wrote a short post on internal DSL in ActionScript)
In my opinion the problem with internal DSL approach is that you can mix the code inside your scenario declaration and this is not a structural improvement.
When you use an external DSL for testing you have to structure your code and the copy&paste amount of source code keeps to the minimum. If you have another scenario that is pretty similar to one you already wrote you just copy the scenario and changes the parameter. You don’t have to write or duplicate any line of code.

My BDD framework of choice for MockitoViz is JBehave. It’s external DSL based and has a clear structure.

It has three main keywords for Scenario description: Given, When, Then.

If you think about how a test is structured, you see that for a test you have to create a state where the unit under test will be used. It could be the instantiation of the class you would like to test, or creation of the input if it is a complex one.

The creation of the state should be described under the keyword Given.

Given a Compilation Unit from this source code:
package foo;
public class A {
  public void bar(){
    A a = mock(A.class)
And a visitor to FindMockedObjects with stubbed BindingHandler
And stubbed BindingHandler witch returns 'A' as type

The And keyword can be used so you don’t have to write Given every time.

After that you want to fire up an action that will give you some output you wan’t to test. In other word’s you want to use your program under test.

This should be described under the keyword When

When the visitor have visited the AST

Now we can write done our expectations leaded with the keyword Then

Then found mocks should not be empty
And 1 mock should be found
And mocks name should be 'a' 
And mocks type should be 'A'

Leave a Reply



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Before you submit form:
Human test by Not Captcha

© 2011 Max Blog Suffusion theme by Sayontan Sinha