jbehave tutorial

26
By Megha Mujumdar JBehave JBehave

Upload: megha-mujumdar

Post on 12-Dec-2014

1.315 views

Category:

Documents


8 download

DESCRIPTION

JBehave Tutorial.It covers the basic and advanced topics on JBehave.

TRANSCRIPT

Page 1: JBehave Tutorial

• By Megha Mujumdar

JBehaveJBehave

Page 2: JBehave Tutorial

• TDD – (Test Driven Development) • BDD – (Behavior Driven Development)• JBehave Overview• JBehave Setup• Examples

AgendaAgenda

Page 3: JBehave Tutorial

• Test-driven development (TDD), also called test-driven design, is a method of software development in which unit testing is repeatedly done on source code.

• After each test, refactoring is done and then the same or a similar test is performed again. The process is iterated until each unit is functioning according to the desired specifications.

TDD:- TEST DRIVEN

DEVELOPMENT

Page 4: JBehave Tutorial

TDD:- TEST DRIVEN DEVELOPMENT

Page 5: JBehave Tutorial

• BDD - Behavior Driven Development is a Software development based on Test Driven Development (TDD)

• BDD is extension of TDD done for testing the intent of the system rather than testing a particular piece of code.

BDD (Acceptance TDD)- BEHAVIOR

DRIVEN DEVELOPMENT

Page 6: JBehave Tutorial

BDD (Acceptance TDD)- BEHAVIOR

DRIVEN DEVELOPMENT

Page 7: JBehave Tutorial

• Test method names should be sentences.• Simple sentence template.• An expressive test name is helpful• Requirements determine the behaviors• Standard language (Given, when, then)

Philosophy of BDD

Page 8: JBehave Tutorial

• JBehave is a framework for Behavior Driven Development(BDD) with acceptance testing.

JBehaveJBehave

Page 9: JBehave Tutorial

JBehaveJBehave

Page 10: JBehave Tutorial

JBehaveJBehave

Page 11: JBehave Tutorial

• BDD encourages you to start defining the stories via scenarios that express the desired behaviour in a textual format

• Story - A story is a collection of scenarios, each detailing different examples of the behaviour of a given increment of functionality of the system.

• Map Steps to Java:- JBehave maps textual steps to Java methods via CandidateSteps.

• JBehave uses method annotations in Java classes to associate an executable Java method to a StepCandidate. Each step candidate corresponds to one Java method and to one StepType.

• Configure Stories:- JBehave allows many different ways to configure Embeddable Java classes that allow the parsing and running of textual stories.

• Story Reporters:- Story reporting allows to monitor the outcome of the stories that have been run.

JBehaveJBehave

Page 12: JBehave Tutorial

• Download the JBehave core jar:-ohttp://mvnrepository.com/artifact/

org.jbehave/jbehave-core/3.0-rc1• Put the jar file in your project workspace• Create and run a simple scenario• Three files required:

oScenario text(story) file with given-when-then format with .story extension

o Java/file class to extend Scenario fileo Java steps file for the method shells

Getting started with simple JBehave

project

Page 13: JBehave Tutorial

Narrative: In order to communicate effectively to the business some functionality As a development team

I want to use Behaviour-Driven Development

Scenario: A scenario is a collection of executable steps of different type

Given step represents a precondition to an eventWhen step represents the occurrence of the eventThen step represents the outcome of the event

Scenario: Another scenario exploring different combination of events

Given a precondition When a negative event occurs Then a the outcome should be captured

JBehave: JBehave: Sample storySample story

Page 14: JBehave Tutorial

Narrative: In order to learn and enjoy the delicious pancakesAs a food loverI want to make pancakes

Scenario: Making pancakes in a skilletGiven some pancake batterAnd a hot frying panWhen I pour the batter into the frying pan

•And wait for 2 minutes or until the sides are dry•And flip the pancake over•And wait for 1 minute•Then I get a cooked pancake

JBehave: Story File:- JBehave: Story File:-

Make_pancakes.story Make_pancakes.story

Page 15: JBehave Tutorial

package com.cse.simple.steps;import org.jbehave.core.annotations.Given;import org.jbehave.core.annotations.Then;import org.jbehave.core.annotations.When;public class PancakeCookingSteps {

@Given(“some pancake batter”)public void howMuchBatter() {

// add code for amount of batter }@Given(“hot frying pan”)public void heatFryingPan() {

// add code to determine heat }@When(“I pour the batter into the frying pan”)public void startCooking() {

// add code to start cooking }}

Java steps

file :PancakeCookingSteps.java(1/2)

Page 16: JBehave Tutorial

@When(“wait for 2 minutes or until the sides are dry”)public void cookFirstSide() {

// add code to cook first side}@When(“flip the pancake”)public void flipPancake() {

// add code to flip pancake}@When(“wait for 1 minute”)public void cookSecondSide() {

// add code to cook second side}@Then(“I get a cooked pancake”)public void putPancakeOnPlate() {

// add code to move pancake from frying pan to plate}

}

Java steps

file :PancakeCookingSteps.java(2/2)

Page 17: JBehave Tutorial

Parameter ConvertersJBehave automatically converts Automatic conversion of textual values to Java objects in the steps instance class.

Given a stock of symbol STK1 and a threshold of 10.0@Given("a stock of symbol $symbol and a threshold of $threshold")

public void aStock(String symbol, double threshold) {    // ... }Even if we have comma separated values it would be automatically handled.

Given a stock of symbols STK1,STK2 and thresholds of 10.0,20.0 is traded on 09/09/2009@Given("a stock of symbol $symbol and a threshold of $threshold" is traded on $tradedOn")

public void aStock(String symbol, double threshold, Date tradedOn) {    // ... }

Advanced JBehaveAdvanced JBehave

Page 18: JBehave Tutorial

Parameter InjectionJBehave supports multiple mechanisms for parameter injection. 1. Ordered Parameters:- This is the default behaviour. The arguments extracted from the step candidate are simply matched following natural order to the parameters in the annotated Java method

Given a threshold with value 2@Given("a threshold with value $value")public void givenThreshold(int value) {

threshold = value;}

Advanced JBehaveAdvanced JBehave

Page 19: JBehave Tutorial

Annotated Named ParametersIf we want to have named parameters, one mechanism is to use annotations:

@Given("a threshold with value $value")public void givenThreshold(@Named("value") int value) {

threshold = value; }Advantage is that we can have method parameter appearing in any order.

Paranamer Named ParametersAn equivalent way to use named parameters, without using annotations, is to leverage Paranamer, configured via the Configuration: Paranamer paranamer = new CachingParanamer(new BytecodeReadingParanamer());StepConfiguration configuration = new Configuration();configuration.useParanamer(paranamer); 

Advanced JBehaveAdvanced JBehave

Page 20: JBehave Tutorial

 Parameterised Scenarios:

Given a stock of symbol STK1 and a threshold of 10.0When the stock is traded at 5.0Then the alert status should be OFFWhen the stock is traded at 11.0Then the alert status should be ON

Parametrised scenario:-Given a stock of <symbol> and a <threshold>When the stock is traded at <price>Then the alert status should be <status>

Examples:     |symbol|threshold|price|status||STK1|10.0|5.0|OFF||STK1|10.0|11.0|ON| The Examples: keyword signals that the entire scenario is parametrised and should be repeated for as many times as there are data rows in the examples table.

Advanced JBehaveAdvanced JBehave

Page 21: JBehave Tutorial

Reusing Textual Stories - GivenStories

GivenStories: Dashboard_Database/MU_Services_Database_Organization_Connection.story

Scenario: verify the enterpriseName When JBehave encounters the keyword GivenStories, it will run the

(one or more) textual stories specified by path in the list (using the same Steps instances specified for the parent story) before proceeding to the execution of the scenarios

Advanced JBehaveAdvanced JBehave

Page 22: JBehave Tutorial

Pending StepsPending steps are steps that do not match any public Java method in the steps classes. Steps marked as NOT PERFORMED which occur after a step failure.You can also fail the scenario if there is any Pending steps using:-

Configuration configuration = new MostUsefulConfiguration()

     .usePendingStepStrategy(new FailingUponPendingStep()); Using @Pending annotations you can tell JBehave that the step has yet to be implemented. @Given("a stock of symbol $symbol and a threshold of $threshold")@Pending

public void aStock(String symbol, double threshold) {    // not yet implemented}

Advanced JBehaveAdvanced JBehave

Page 23: JBehave Tutorial

o Meta Info To help users better manage their stories, JBehave allow the specification of meta information, both at story and scenario level. Meta information is provided as a list of name-value properties.

Meta:@Sprint 6RT2@Type positive@Story HAP-7582@Run yes@author John

Advanced JBehaveAdvanced JBehave

Page 24: JBehave Tutorial

Custom parameter delimitersThe parameter name delimiters are purely conventional. The step matching works just as well if other delimiters - e.g. [] - are used, provided the patterns are updated accordingly: 1. Given a stock of [symbol] and a [threshold]2. Given a stock of <symbol> and a <threshold>3. @Given("a stock of $symbol and a $threshold") Another use case of name delimited parameters is to reuse the same step for different parameter values (with different names), e.g.:

Given a stock of <symbol> and a <threshold>And a stock of <second_symbol> and a <threshold>Examples:     |symbol|alternate_symbol|threshold||STK1|ALT1|1.0|

Advanced JBehaveAdvanced JBehave

Page 25: JBehave Tutorial

  Pattern VariantsWhile aliases allow to explicitly define alternative patterns that match the same executable method, it is cumbersome to list all the different patterns when the difference between them is small.

To overcome this, JBehave supports pattern variant directives. The pattern variants are built by the PatternVariantBuilder.

e.g. A {x|y|z} B => A x B, A y B, A z B When the item cost is 10.0When the price is 10.0When the cost is 10.0 @When("the {item |}{price|cost} is $price")public void theItemPriceIs(double price) {    // ... }

Advanced JBehaveAdvanced JBehave

Page 26: JBehave Tutorial

Thank You