x Java Java 8 JUnit JSON
  • XML
  • JDBC Spring Boot Microservices React Contact Us

    Cucumber Java tutorial

    TDD approach helps in testing the code, but it doesn't help in testing business or funtional scenarios. BDD (behavior-driven development) focuses on specific business features based on scenarios. It also helps in bridging gap between buisness, developers and testers because all agree to a common language in which BDD tests are written.

    In BDD test scenarios are written using Given/When/Then keywords and below format.

    Givenin two numbers
    When subtract one number from other
    Then correct difference should br returned

    Cucumber is a BDD testing framework. which uses Gherkin language to describes application scenarios. In this tutorial we will show how to write a BDD test using Cucumber, Java and JUnit.

    Step 1) Add below dependencies to pom.xml

    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>1.2.5</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>info.cukes</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>1.2.5</version>
        <scope>test</scope>
    </dependency>    

    Step 2) Install Cucumber plugin for eclipse if using eclipse

    Go to Help → Eclipse Marketplace and install Cucumber plugin

    Step 3) Write Calculator Class to test

    Write Calculator class which has subtract method that will be tested using BDD approach

    public class Calculator {
        public int subtract(int a, int b) {
            return a-b;
        }
    }    

    Step 4) Write calculator.feature file under src/test/resources/features directory

    Feature: calculate subtraction of two numbers
       Pass two numbers and check difference
    
      Scenario: Check difference between numbers
        Given two numbers
        When subtract one number from other
        Then correct difference should br returned    

    Step 5) Run feature file

    Right click on calculator.feature file and run it as Cucumber feature. It will provide the skeleton of the test methods as below

    You can implement missing steps with the snippets below:
    
    @Given("^two numbers$")
    public void two_numbers() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }
    
    @When("^subtract one number from other$")
    public void subtract_one_number_from_other() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }
    
    @Then("^correct difference should br returned$")
    public void correct_difference_should_br_returned() throws Throwable {
        // Write code here that turns the phrase above into concrete actions
        throw new PendingException();
    }

    Step 6) Add test class that based on business logic in previous step

    import static org.junit.Assert.assertEquals;
    
    import calculator.Calculator;
    import cucumber.api.java.en.Given;
    import cucumber.api.java.en.Then;
    import cucumber.api.java.en.When;
    
    public class CalculatorStepDef {
        
        private int a;
        private int b;
        private int c;
        private Calculator calculator = new Calculator();  
        
        @Given("^two numbers$")
        public void two_numbers() {
            a = 10;
            b = 4;
        }
    
        @When("^subtract one number from other$")
        public void subtract_one_number_from_other() throws Throwable {
            c = calculator.subtract(a, b);
        }
    
        @Then("^correct difference should br returned$")
        public void correct_difference_should_br_returned() throws Throwable {
            assertEquals(6,c);
        }
    
    }

    Step 7) Add CalculatorCucumberTest class that will run test cases of CalculatorStepDef

    import org.junit.runner.RunWith;
     
    
    import cucumber.api.CucumberOptions;
    import cucumber.api.junit.Cucumber;
     
     
    @RunWith(Cucumber.class)
    @CucumberOptions(features = "src/test/resources/features/calculator.feature",
    glue = "stepDefinitions")
    public class CalculatorCucumberTest {    
    }

    Step 8) Run Cucumber tests CalculatorCucumberTest

    import org.junit.runner.RunWith;
     
    
    import cucumber.api.CucumberOptions;
    import cucumber.api.junit.Cucumber;
     
     
    @RunWith(Cucumber.class)
    @CucumberOptions(features = "src/test/resources/features/calculator.feature", 
        glue = "stepDefinitions")
    public class CalculatorCucumberTest {    
    }

    Output :

        
    1 Scenarios ([32m1 passed[0m)
    3 Steps ([32m3 passed[0m)
    0m0.220s 


    Comments

    Leave a Reply

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











    Share This