Skip to main content

First Cucumber+java+maven project.



Prerequisite: following should be installed:
  • Java
  • Eclipse
  • Maven
  • maven-eclipse-plugin
  • Cucumber-eclipse-plugin


Step 1) Lets create a new project in eclipse by following steps:

Click on new –> Other –> Maven –> Maven Project — > Next

Step 2) Now click on Simple project and keep the default workspace location.

Step 3) Provide details as Artifact id, Group id, name and description. and click on Finish.

Step 4) This should create a basic maven project. 

Create packages as shown in the below snapshot and create new files in the project.






























Feature file:


Feature:  Adding numbers with a Calculator
  In order to not learn math
  As someone who is bad at math
  I want to be able to add numbers using a Calculator
  
  Scenario:  Add two positive numbers
    Given I have a Calculator
    When I add 1 and 1
    Then the sum should be 2
    
  Scenario:  Add a positive and negative number
    Given I have a Calculator
    When I add 1 and -1
    Then the sum should be 0
    
  Scenario:  Add two negative numbers
    Given I have a Calculator
    When I add -1 and -1
    Then the sum should be -2




Runner.java:

@CucumberOptions
(
features = "src/test/resources/featurefile/caclculator.feature",
glue = {"com.steps"},
plugin = {"pretty", "com.cucumber.listener.ExtentCucumberFormatter:target/cucumber-reports/report.html"},
tags={"~@ignore"} 
)

This class responsible for reading the features file 
Feature -- link of feature file
Glue Tag -- This holds the package containing steps
Plugin -- any report plugin you want to add.
Tags -- if you want to test specific tags methods like @functional @regression


package com.feature;

import org.junit.AfterClass;
import org.junit.runner.RunWith;

import com.cucumber.listener.Reporter;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(value = Cucumber.class)
@CucumberOptions
(features = "src/test/resources/featurefile/caclculator.feature",
glue = {"com.steps"},
plugin = {"pretty", "com.cucumber.listener.ExtentCucumberFormatter:target/cucumber-reports/report.html"},
tags={"~@ignore"} 
)
public class Runnner {

    @AfterClass
    public static void writeExtentReport() {
        Reporter.setSystemInfo("Machine", "Windows 7:-" + "64 Bit");
        Reporter.setSystemInfo("Maven", "3.5.2");
        Reporter.setSystemInfo("Java Version", "1.8.0_151");
    }
}



CalculatorSteps.java :



package com.steps;

import static org.junit.Assert.assertEquals;

import com.feature.Calculator;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;


public class CalculatorSteps {

 private Calculator calculator;

 private int actualSum;
        //  ^ - denotes start of string in feature file given statement
 //  $ - denotes end of string in feature file given statement

 @Given("^I have a Calculator$")
 public void intializeCalculator() {
  this.calculator = new Calculator();
 }
        //  ^ - denotes start of string in feature file when statement
 //  $ - denotes end of string in feature file when statement
        //  (-?\\d)  denotes the parameter in number format

 @When("^I add (-?\\d) and (-?\\d)$")
 public void whenIAddTwoNumbers(int firstNumber, int secondNumber) {
  this.actualSum = this.calculator.add(firstNumber, secondNumber);
 }

 @Then("^the sum should be (-?\\d)$")
 public void thenTheSumShouldBe(int expectedSum) {
  assertEquals("The expected sum does not equal the actual sum", expectedSum, this.actualSum);
 }
}




Calcuator.java:

This class is our main class which we need to test using our feature file.This contains our logical code generally.


package com.feature;

public class Calculator {

 public int add(int firstNumber, int secondNumber) {
  return firstNumber + secondNumber;
 }
}



Pom.xml :
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.practise</groupId>
 <artifactId>cucumber</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <description> </description>
 <properties>
  <cucumber.version>0.3.2</cucumber.version>
  <file.encoding>UTF-8</file.encoding>
  <java.version>1.8</java.version>
 </properties>
 <build>
  <plugins>
   <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>2.3.2</version>
    <configuration>
     <source>${jdk.version}</source>
     <target>${jdk.version}</target>
     <encoding>${file.encoding}</encoding>
    </configuration>
   </plugin>
  </plugins>
 </build>
 <dependencies>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>2.5.6</version>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.12</version>
   <scope>test</scope>
  </dependency>
  
  
  <!-- Cucummber dependencies -->
  
  <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>1.2.5</version>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>1.2.5</version>
        </dependency>
        
         <!-- https://mvnrepository.com/artifact/com.vimalselvam/cucumber-extentsreport -->
        <dependency>
            <groupId>com.vimalselvam</groupId>
            <artifactId>cucumber-extentsreport</artifactId>
            <version>3.0.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.aventstack/extentreports -->
        <dependency>
            <groupId>com.aventstack</groupId>
            <artifactId>extentreports</artifactId>
            <version>3.1.2</version>
        </dependency>
  
 </dependencies>

</project>



As  you addded all these class and files now time to run the application

open class Runner and rightclick ->run as -->Junit test

You will see this on screen with all passing test cases.



Flow diagram of cucumber test.







Comments

Popular posts from this blog

Extent report plugin for cucumber framework

Extent Reports  are the most popular  reporting  used with Selenium. ExtentReport API makes our life easy to generate interactive  report  with simple configuartions. It supports almost all Java and .NET test frameworks such as TestNG , JUnit , NUnit etc Here we are discussing about  a plugin which is build on  Extent Report specially for Cucumber. This plugin is used to simple out the implementation of  Extent Report  in  Cucumber Framework .  We are creating a maven project to implement the integration of our plugin with cucumber 1. Create new maven project in any tool eclipse/sts/intellij 2. Open pom.xml and update below entries. Step 1 : Add Cucumber Extent Reporter library to Maven Project Add  cucumber-extentsreport <dependency>      <groupId> com.vimalselvam </groupId>      <artifactId> cucumber-extentsreport </artif...

java: You aren't using a compiler supported by lombok, so lombok will not work and has been disabled.

  In order to make projects compile with the existing builds of Lombok processor, as a workaround you can use the flag -Djps.track.ap.dependencies=false which should be added to File | Settings | Build, Execution, Deployment | Compiler | Build process VM options field. This will disable collection of dependencies specified by an annotation processor when Filer methods are called

Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.2.3.RELEASE:repackage failed: Unable to find main class

Solutions:  Solution 1 : You needed to change the packaging parameter to jar from pom. Also, the repositories , pluginRepositories , the maven-compiler-plugin and the spring-boot-maven-plugin's version and executions weren't needed. Solution 2:  Try mvn install and see if it works Solution 3: Preview: <properties> <!-- The main class to start by executing java -jar --> <start-class> com.mycorp.starter.HelloWorldApplication </start-class> </properties> Solution 4: Enable the main() method in your Application.java. Configure spring-boot-maven-plugin to specify the class with the main class (Spring should find it anyway if you have one, but good to be explicit): Preview: <plugin> <groupId> org.springframework.boot </groupId> <artifactId> spring-boot-maven-plugin </artifactId> <version> ${spring-boot-version} </version>...