Tuesday, August 16, 2011

JBehave - quick BDD example

After few last months of work on some projects with Spring MVC, Spring WebFlow, and JPA I had to do something else ;) - even for a while ;) - below you may find a results of my small experiments with Behavior Driven Development and JBehave :)

I started with Introducing BDD and What's in a Story? articles by Dan North, and Alex Soto blog entry about Spring Framework, JBehave and Selenium 2 integration.

Then I asked myself: "What I need for the BDD experiment? - The Story :) - I borrowed one from Dan's article What's in a Story?. Suppose that we have an Account, Card bound to this account, and ATM which will dispense some money. In order to get money when the bank is closed, as an account holder I want to withdraw cash from an ATM - this is the story :) - This story will have 3 possible scenarios in my example:


As you see, this story and the scenarios included are written in human language :) - no programming here, it can be written by anyone! OK, but where is the development here?! - Give me few minutes, and I'll explain that :)

Each Scenario consist of 3 steps: Given, When, Then. First one (Given) defines the context of the specific scenario (ex. "the card is disabled"), second (When) defines the event which occurred in this specific context (ex. "the account holder requests 20"), and third one (Then) defines the state of the context after the event processing (ex. "the ATM should retain the card").

Each scenario's step is bound to the Java code responsible for "implementing" it, in our example:


When you look at the @Given, @When, and @Then annotations you'll see that their values are matching the content of scenario steps written in human language - and this is the point where human language used by your Business Development Team meets the "Real Developers" ;)

Now what? - the rest is easy :) - Alex Soto proposed in his article (Spring Framework, JBehave and Selenium 2 integration) that we can use the Spring Framework driven Components for holding the Steps, this is very nice idea, and sufficient in my example (I would worry about it only in multi-threaded environment, because Component build by Spring is a Singleton by default, and that's usually not a good idea in such  environment).

Spring Framework configuration:


We perform the component scan here (which will register bean for our Steps holding class), and define only one bean related to JBehave, configuring its report builder.

Now we need a code which will allow us to run the Story processing as JUnit test:

When you run this test, all scenarios will be verified, and along with the JUnit test result, JBehave report will be produced:

Few links for the dessert:

4 comments:

  1. I've been reading on this subject and I'm still not clear on whether these tests should target the UI (using Selenium / WebDriver), the service layer or some test-dedicated layer just above the service layer. UI tests replicate the actual usage of the application, but target its most brittle part. Service layer tests involve no additional code, but the service layer may need to be tweaked to accommodate test peculiarities. And a dedicated layer above the service layer seems like a logical approach but it requires some additional development and costs. What's your take on this?

    ReplyDelete
  2. @Spiff - long time since you entered your comment ... but maybe my response will help you somehow anyway ;) - let me say that I was playing with BDD mostly in the terms of the web application - thus the UI layer testing, but it can be very useful in testing the service layer too - the choice depends on specific application/code

    ReplyDelete
  3. I just want to know,how calls are made for html report file which user see as report of story execution?
    I want to update html-output.ftl file such a way that when final report gets generated in
    ..\target\jbehave\view\reports.html file, i don't want columns like Given-story Scenarios, Steps in html output file as I want to delete those permanently ..how i can do that?
    When i see the html-output.ftl file it has very complex code which does not get how the columns are mapped and how to delete the what code...
    ...do I need to delete the code in jbehave-core.css file
    as well? Pls suggest
    Also if i made changes in code , how i can make them persistent/permanent so that every time i compile, i don't see old code...

    ReplyDelete
  4. Is it possible to implement the TestNG along with Jbehave? Could you please share sample code?

    ReplyDelete