JBehave Code Generator Manual Contents: 1) Introduction & Installation 2) Why we need Code Generator. 3) How to generate code with example.
Introduction JBehave is a framework for Behaviour-Driven Development (BDD). BDD is an evolution of test-driven development (TDD) and acceptance-test driven design, and is intended to make these practices more accessible and intuitive to newcomers and experts alike. It shifts the vocabulary from being test-based to Behaviourbased, and positions itself as a design philosophy. Installation of eclipse plugin. 1) Download JBehaveCodeGenerator from http://www.mazataz.com/resources/org.custom.jbehave.c ode.generator_2.0.2.jar 2) Place it in eclipse plugin directory 3) Restart eclipse.
Why we need code generator. The business and technical team generate JBehave scenario in text files. The testers or developers would have to write the stub classes for the behavior manually. This was error prone and a learning curve for testers. To bridge this gap and get non-java testers on board for BDD we developed an eclipse plugin that would automatically generate the scenario and step java files. Then tester and developers can integrate the steps with respective system under test. Here is a pictorial representation of JBehave framework.
How to generate code Here is an example to show how to generate code for JBehave scenario. Lets take a sample user story: Story Title: Login to the Customer Service Centre (CSC) So That: I can resolve customer issue with an order As: A user I Need: To Login to the CSC Acceptance Criteria: 1. I should be able to login using valid username and password 2. I should not be able to login using invalid username or password Lets break it down into scenarios for 1 st acceptance criteria Scenario 1: Valid Login Given the user is on Login Page When the user types user name service And the user types password service And clicks login button Then the user should be logged in And the user should see a message, Welcome, Service Administrator. 1) Create valid_login.scenario file in eclipse. 2) Copy login scenario into it and save. 3) Right click on this file and choose a. JBehaveCodeGenerator i. Generate Code 4) This will create two java files a. ValidLogin.java b. ValidLoginSteps.java
ValidLogin.java public class ValidLogin extends Scenario { public ValidLogin () { super(new ValidLoginSteps()); Modify ValidLogin.java so that it extends BddCommonScenario from below, so that.scenario files can be executed. package bdd.common.scenario; import org.jbehave.scenario.mostusefulconfiguration; import org.jbehave.scenario.scenario; import org.jbehave.scenario.errors.pendingerrorstrategy; import org.jbehave.scenario.parser.classpathscenariodefiner; import org.jbehave.scenario.parser.patternscenarioparser; import org.jbehave.scenario.parser.scenariodefiner; import org.jbehave.scenario.parser.underscoredcamelcaseresolver; public class BddCommonScenario extends Scenario{ public BddCommonScenario() { super(new MostUsefulConfiguration() { public ScenarioDefiner fordefiningscenarios() { return new ClasspathScenarioDefiner(new UnderscoredCamelCaseResolver(".scenario"), new PatternScenarioParser(keywords())); public PendingErrorStrategy forpendingsteps(){ return PendingErrorStrategy.FAILING; );
ValidLoginSteps.java import org.jbehave.scenario.annotations.*; import org.jbehave.scenario.steps.steps; public class ValidLoginSteps extends Steps { @Given("the user is on the Login Page") public void theuserisontheloginpage() { @When("the user types username $username") public void theusertypesusername(string username) { @When("the user types password $password") public void theusertypespassword(string password) { @When("clicks login button") public void clicksloginbutton() { @Then( the use should be logged in") public void theusershouldbeloggedin() { Now start implementing the steps.