Getting Started with Cucumber Java – BDD

Getting started with Cucumber JVM

BDD – Its one of the most trending topics in the Test Automation world, while Adoption of Agile in its way.
To explain about BDD, there are lot of other sources around the web and am not going to explain them here, rather sticking to only explain the Java way of Cucumber.

Cucumber

There is no any setup programs available to get started with cucumber -jvm , as its Java its all about jar’s.

It’s as simple as create a Maven project and add dependencies relating to cucumber.

Let’s get into practical setup!

Cucumber set up

1. Create a new Maven Project

newMvnproj

2. Package structure

sanpshotstructure

3. Add the below dependencies to the pom.xml under ” <dependencies> </dependencies> ”

4. Execute the pom.xml mvn clean install

In Eclipse console you could see the progress, that it will automatically download the dependencies from macgyver nexus repo.
mvn clean install will tell Maven to do the clean action in each module before triggering the install action for each module.

As shown in below Screenshot you should get to see BUILD SUCCESS message
build success

5. If you refresh the workspace you could see the Maven Dependencies are added to your project.

mvnDependencies

6. Create the Cucumber Runner class

  • Create a new class under src/test/java package and name it as say: CucumberRunner.java
  • Add the below code snipper to the class
  • It will show error’s in the beginning and its expected.
    import error

    7. Import packages to resolve error.

  • Mouse hover on @Runwith and import the related packages
  • If you notice the red line error is vanished and you could see an import package declaration have been made
  • runwithresolved

  • Request you to do the same for Cucumber.class as well.
  • Next step is, you would still see the a red line error on @CucumberOptions , that’s because we have reference the cucumber core jar to the project.
    Easy way to do is, Mouse Hove on @CucumberOptions and click on fix setup as shown in screen shot below and you are done.
  • addcukecore

  • Refresh the workspace and you could see cucumber -core added under Referenced Libraries section
  • 8. Let’s create a Source folder to place our Gherkin file.

  • Create a new source folder and name it as src/test/resources
  • sourcefolder

  • Create a new file with name say AndroidSearch.feature – Here its important to save the file with .feature extension.
  • featuresnap
    The Given When And & Then’s

    Example

    with that the basic setup is completed for Cucumber Java, Now let’s proceed writing a basic test and its step definitions

    9. Run the feature as Cucumber test.

    runascuketest

    10. In the console you would see the cucumber is expecting these step definitions to be implemented.

    stepDefinitions

    11. Create a new java file called StepDefinitions.java and paste the snippet that cucumber expects to run as in above screenshot. As usual, mouse Hover and resolve the import errors.
    stepDefinition1

    12. In src/main/java you can create your packages or classes however you want so that it satisfies and implements all the Gherkin steps.
    Page object model can be implemented here, with having all the individual pages implemented here and called in Step Definitions.

    Will Follow up with another post, on How to use Page objects in this cucumber setup.

    Hope that helps !!

    2 thoughts on “Getting Started with Cucumber Java – BDD

    Leave a Comment

    *