JBehave Tutorial
Upcoming SlideShare
Loading in...5
×
 

JBehave Tutorial

on

  • 672 views

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

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

Statistics

Views

Total Views
672
Views on SlideShare
672
Embed Views
0

Actions

Likes
1
Downloads
23
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Client&apos;s reaction when exception happens <br /> Exception type <br /> 1.Client code cannot do anything <br /> 1.Make it an unchecked exception <br /> 2.Client code will take some useful recovery <br /> 2. Make it a checked action based on information in exception <br />
  • Client&apos;s reaction when exception happens <br /> Exception type <br /> 1.Client code cannot do anything <br /> 1.Make it an unchecked exception <br /> 2.Client code will take some useful recovery <br /> 2. Make it a checked action based on information in exception <br />
  • Client&apos;s reaction when exception happens <br /> Exception type <br /> 1.Client code cannot do anything <br /> 1.Make it an unchecked exception <br /> 2.Client code will take some useful recovery <br /> 2. Make it a checked action based on information in exception <br />
  • Client&apos;s reaction when exception happens <br /> Exception type <br /> 1.Client code cannot do anything <br /> 1.Make it an unchecked exception <br /> 2.Client code will take some useful recovery <br /> 2. Make it a checked action based on information in exception <br />
  • Client&apos;s reaction when exception happens <br /> Exception type <br /> 1.Client code cannot do anything <br /> 1.Make it an unchecked exception <br /> 2.Client code will take some useful recovery <br /> 2. Make it a checked action based on information in exception <br />
  • Client&apos;s reaction when exception happens <br /> Exception type <br /> 1.Client code cannot do anything <br /> 1.Make it an unchecked exception <br /> 2.Client code will take some useful recovery <br /> 2. Make it a checked action based on information in exception <br />
  • Client&apos;s reaction when exception happens <br /> Exception type <br /> 1.Client code cannot do anything <br /> 1.Make it an unchecked exception <br /> 2.Client code will take some useful recovery <br /> 2. Make it a checked action based on information in exception <br />
  • Client&apos;s reaction when exception happens <br /> Exception type <br /> 1.Client code cannot do anything <br /> 1.Make it an unchecked exception <br /> 2.Client code will take some useful recovery <br /> 2. Make it a checked action based on information in exception <br />
  • Client&apos;s reaction when exception happens <br /> Exception type <br /> 1.Client code cannot do anything <br /> 1.Make it an unchecked exception <br /> 2.Client code will take some useful recovery <br /> 2. Make it a checked action based on information in exception <br />
  • Client&apos;s reaction when exception happens <br /> Exception type <br /> 1.Client code cannot do anything <br /> 1.Make it an unchecked exception <br /> 2.Client code will take some useful recovery <br /> 2. Make it a checked action based on information in exception <br />
  • Client&apos;s reaction when exception happens <br /> Exception type <br /> 1.Client code cannot do anything <br /> 1.Make it an unchecked exception <br /> 2.Client code will take some useful recovery <br /> 2. Make it a checked action based on information in exception <br />
  • Client&apos;s reaction when exception happens <br /> Exception type <br /> 1.Client code cannot do anything <br /> 1.Make it an unchecked exception <br /> 2.Client code will take some useful recovery <br /> 2. Make it a checked action based on information in exception <br />
  • Client&apos;s reaction when exception happens <br /> Exception type <br /> 1.Client code cannot do anything <br /> 1.Make it an unchecked exception <br /> 2.Client code will take some useful recovery <br /> 2. Make it a checked action based on information in exception <br />
  • Client&apos;s reaction when exception happens <br /> Exception type <br /> 1.Client code cannot do anything <br /> 1.Make it an unchecked exception <br /> 2.Client code will take some useful recovery <br /> 2. Make it a checked action based on information in exception <br />
  • Client&apos;s reaction when exception happens <br /> Exception type <br /> 1.Client code cannot do anything <br /> 1.Make it an unchecked exception <br /> 2.Client code will take some useful recovery <br /> 2. Make it a checked action based on information in exception <br />
  • Client&apos;s reaction when exception happens <br /> Exception type <br /> 1.Client code cannot do anything <br /> 1.Make it an unchecked exception <br /> 2.Client code will take some useful recovery <br /> 2. Make it a checked action based on information in exception <br />
  • Client&apos;s reaction when exception happens <br /> Exception type <br /> 1.Client code cannot do anything <br /> 1.Make it an unchecked exception <br /> 2.Client code will take some useful recovery <br /> 2. Make it a checked action based on information in exception <br />
  • Client&apos;s reaction when exception happens <br /> Exception type <br /> 1.Client code cannot do anything <br /> 1.Make it an unchecked exception <br /> 2.Client code will take some useful recovery <br /> 2. Make it a checked action based on information in exception <br />
  • Client&apos;s reaction when exception happens <br /> Exception type <br /> 1.Client code cannot do anything <br /> 1.Make it an unchecked exception <br /> 2.Client code will take some useful recovery <br /> 2. Make it a checked action based on information in exception <br />
  • Client&apos;s reaction when exception happens <br /> Exception type <br /> 1.Client code cannot do anything <br /> 1.Make it an unchecked exception <br /> 2.Client code will take some useful recovery <br /> 2. Make it a checked action based on information in exception <br />
  • Client&apos;s reaction when exception happens <br /> Exception type <br /> 1.Client code cannot do anything <br /> 1.Make it an unchecked exception <br /> 2.Client code will take some useful recovery <br /> 2. Make it a checked action based on information in exception <br />
  • Client&apos;s reaction when exception happens <br /> Exception type <br /> 1.Client code cannot do anything <br /> 1.Make it an unchecked exception <br /> 2.Client code will take some useful recovery <br /> 2. Make it a checked action based on information in exception <br />
  • Client&apos;s reaction when exception happens <br /> Exception type <br /> 1.Client code cannot do anything <br /> 1.Make it an unchecked exception <br /> 2.Client code will take some useful recovery <br /> 2. Make it a checked action based on information in exception <br />
  • Client&apos;s reaction when exception happens <br /> Exception type <br /> 1.Client code cannot do anything <br /> 1.Make it an unchecked exception <br /> 2.Client code will take some useful recovery <br /> 2. Make it a checked action based on information in exception <br />

JBehave Tutorial JBehave Tutorial Presentation Transcript

  • JJBBeehhaavvee • By Megha Mujumdar
  • AAggeennddaa • TDD – (Test Driven Development) • BDD – (Behavior Driven Development) • JBehave Overview • JBehave Setup • Examples
  • TDD:- TEST DRIVEN DEVELOPMENT • 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
  • DEVELOPMENT • 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.
  • DEVELOPMENT
  • • Test method names should be sentences. • Simple sentence template. • An expressive test name is helpful • Requirements determine the behaviors • Standard language (Given, when, then)
  • JJBBeehhaavvee • JBehave is a framework for Behavior Driven Development(BDD) with acceptance testing.
  • JJBBeehhaavvee
  • JJBBeehhaavvee
  • JJBBeehhaavvee • 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.
  • Getting started with simple JBehave project • Download the JBehave core jar:- ohttp://mvnrepository.com/artifact/org.jb ehave/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 oJava/file class to extend Scenario file oJava steps file for the method shells
  • 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 event When step represents the occurrence of the event Then 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: SSttoorryy FFiillee::-- MMaakkee__ppaannccaakkeess..ssttoorryy Narrative: In order to learn and enjoy the delicious pancakes As a food lover I want to make pancakes Scenario: Making pancakes in a skillet Given some pancake batter And a hot frying pan When 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
  • :PancakeCookingSteps.java(1/2) 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(2/2) @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 } }
  • AAddvvaanncceedd JJBBeehhaavvee Parameter Converters JBehave 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) { // ... }
  • AAddvvaanncceedd JJBBeehhaavvee Parameter Injection JBehave 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; }
  • AAddvvaanncceedd JJBBeehhaavvee Annotated Named Parameters If 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 Parameters An 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);
  • AAddvvaanncceedd JJBBeehhaavvee Parameterised Scenarios: Given a stock of symbol STK1 and a threshold of 10.0 When the stock is traded at 5.0 Then the alert status should be OFF When the stock is traded at 11.0 Then 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.
  • AAddvvaanncceedd JJBBeehhaavvee Reusing Textual Stories - GivenStories GivenStories: Dashboard_Database/MU_Services_Database_Organization_Connectio n.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
  • AAddvvaanncceedd JJBBeehhaavvee Pending Steps Pending 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 }
  • AAddvvaanncceedd JJBBeehhaavvee 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
  • AAddvvaanncceedd JJBBeehhaavvee Custom parameter delimiters The 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|
  • AAddvvaanncceedd JJBBeehhaavvee Pattern Variants While 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.0 When the price is 10.0 When the cost is 10.0 @When("the {item |}{price|cost} is $price") public void theItemPriceIs(double price) { // ... }
  • Thank You