Behavior Driven Development (BDD)

by:

Uncategorized

// Model Entity

public class User {
    private String name;
    private String certification;
    private int marks;

    // getters, setters

    public boolean getResult() {
        if (marks < 60) {
            return false;
        } else {
            return true;
        }
    }
}
// Scenario

Feature: User Certification

  Scenario: User is Passed
    Given that the user Vinod is given a task to clear Java certification exam
    When Vinod got 60 marks in exam
    Then Vinod is known as Java certified
// Cucumber Steps Definitions

public class UserSteps {
    private User user = new User();

    @Given("^that the user (.*) is given a task to clear (.*) certification exam$")
    public void certificationName(String name, String certification) {
        user.setName(name);
        user.setCertification(certification);
    }

    @When("^(.*) got (\\d+) marks in exam$")
    public void gotMarks(String name, int marks) {
        user.setName(name);
        user.setMarks(marks);
    }

    @Then("^(.*) is known as (.*) certified$")
    public void certifiedYes(String name, String certification) {
        assertThat(name, is(user.getName()));
        assertThat(user.getCertification(), equalTo("Java"));
        assertThat(user.getResult(), is(true));
    }
}
// Test class to configure and run Cucumber

@RunWith(Cucumber.class)
@CucumberOptions(
        features = {"classpath:features/user.feature"},
        glue = "com.company.testjunitcucumber")
public class UserTest {
}

Previous Next

Leave a Reply

Your email address will not be published. Required fields are marked *