Search This Blog

Wednesday, 7 October 2015

Configure Cucumber (BDD) in Java

Cucumber Configuration Steps:

Configuring Cucumber BDD tool in Java can be achieved by following the below simple steps: 

Step 1: Install the following softwares
             Java (JDK And JRE) 
             Java IDE - To write the code 
                Maven - Centralized repository for all the jar files


Step 2: Create a simple Maven Project in the Java IDE                    
                The Maven project structure looks like this:
                                <<ProjectName>>
                                |-- pom.xml
                                `-- src
                                       |-- main
                                       |   |-- java
                                       |   `-- resources
                                       `-- test
                                              |-- java
                                              `-- resources



Step 3: (Optional step in most of the IDE. Mandatory in ItelliJIdea Ide)   
                Src -> main -> java - as Source root
                Src -> test -> java - as Test source root
                Src -> test -> resources - as "Resources for Test Source"

Add the following maven dependencies to the POM:
                <dependency>
                                <groupId>info.cukes</groupId>
                                <artifactId>cucumber-java</artifactId>
                                <version>1.2.2</version>
                </dependency>
                <dependency>
                                <groupId>info.cukes</groupId>
                                <artifactId>cucumber-core</artifactId>
                                <version>1.2.2</version>
                </dependency>
                <dependency>
                                <groupId>info.cukes</groupId>
                                <artifactId>cucumber-junit</artifactId>
                                <version>1.2.2</version>
                </dependency>
                <dependency>
                                <groupId>info.cukes</groupId>
                                <artifactId>cucumber-picocontainer</artifactId>
                                <version>1.2.2</version>
                </dependency>

Alternatively: If working on a non-maven project, download the respective jar files and add them to the project build path

Action: Save the POM.xml and clean install
Observation:  This should automatically download the required jar files into the Maven repository.
                                               
Step 4: Create files to resemble the folder structure of the project like below:
projectName
|-- pom.xml
`-- src
    |-- main
    |   `-- java
    |         `-- packageName
    |               `-- Sample.java
    `-- test
        |-- java
        |     `-- packageName
        |                      |-- RunCukesTest.java
        |           `-- Definitions.java
        `-- resources
              `-- packageName
                    `-- demo.feature


Details of the files:
1. Demo.feature is the Cucumber feature file.
                File Description: This contains the cucumber steps in Given When Then format
2. Defintions.java is the bridge between the actual java code and the GWT (Given When Then) format files.
   File Description: This contains the references to the functions which are to be invoked for every step. In general, this type of file(s) can be called as <<featureName>>StepDefs.java
3. RunCukesTest.java is the configuration file which holds details of the "Features Files"
4. Sample.java contains the actual java code. This code can be referenced from definitions.java

The source code of these files will be discussed in detailed

Step 5:
 (Assuming that the code is placed in the respective files)
1.       Open RunCukesTest.java
2.       Run the file as Junit Test
                               
                Observation:
This should trigger the execution.
                        As a result, the test steps defined in "demo.feature" are invoked.
                        Compiler finds the respective step definitions from "Definitions.java"
                        Defintions.java calls the functions/executes the logic to complete the test



No comments:

Post a Comment