This Blog is illustrate how to use JBehavior for Behaviour-Driven Development Testing.
from file->new create new Java Project Set up Project.
Create Calculator Project classes.
package com.ruks.core; public class Calculator { private int sum; public Calculator() { this.sum = 0; } public void addTwoNumber(int x, int y) { sum = x + y; } public int getresult() { return this.sum; } }
package com.ruks.step; import org.jbehave.core.annotations.Given; import org.jbehave.core.annotations.Then; import org.jbehave.core.annotations.When; import junit.framework.Assert; import com.ruks.core.Calculator; public class CalculatorStep { private Calculator myCal; @Given("a calculator") public void setCal() { myCal=new Calculator(); System.out.println("Created"); } @When("I add $number1 and $number2") public void AddCal(int x,int y) { myCal.addTwoNumber(x, y); } @Then("the outcome should $result") public void testResult(int output) { Assert.assertEquals(output, myCal.getresult()); } }
Scenario: CAl add testin Given a calculator When I add 2 and 9 Then the outcome should 11
package com.ruks.story; import java.util.List; import org.jbehave.core.configuration.Configuration; import org.jbehave.core.configuration.MostUsefulConfiguration; import org.jbehave.core.io.LoadFromClasspath; import org.jbehave.core.junit.JUnitStory; import org.jbehave.core.reporters.Format; import org.jbehave.core.reporters.StoryReporterBuilder; import org.jbehave.core.steps.CandidateSteps; import org.jbehave.core.steps.InstanceStepsFactory; import com.ruks.step.CalculatorStep; public class CalculatorStory extends JUnitStory{ @Override public Configuration configuration() { return new MostUsefulConfiguration() // where to find the stories .useStoryLoader(new LoadFromClasspath(this.getClass())) // CONSOLE and TXT reporting .useStoryReporterBuilder( new StoryReporterBuilder().withDefaultFormats() .withFormats(Format.CONSOLE, Format.TXT)); } // Here we specify the steps classes @Override public List‹CandidateSteps› candidateSteps() { // varargs, can have more that one steps classes return new InstanceStepsFactory(configuration(), new CalculatorStep()) .createCandidateSteps(); } }
it seems to be simple calculator BDD project is complete.
Add Comment
Comments (0)