From fd8e872feb9756f80125f0e4893b2ccf77b3494e Mon Sep 17 00:00:00 2001 From: Himanshu Bhardwaj Date: Thu, 6 Apr 2017 14:14:28 +0530 Subject: [PATCH] [BAEL-752] Cucumber with Scenario Outline (#1583) * [BAEL-752] Cucumbr with Scenario Outline * Corrected indentation * Applied more formatting guidelines. * Java standard used for method names --- cucumber/pom.xml | 93 +++++++++++++++++++ .../cucumber/calculator/Calculator.java | 7 ++ .../calculator/CalculatorRunSteps.java | 38 ++++++++ .../cucumber/calculator/CalculatorTest.java | 15 +++ .../calculator-scenario-outline.feature | 16 ++++ .../resources/features/calculator.feature | 24 +++++ cucumber/src/test/resources/logback-test.xml | 13 +++ pom.xml | 1 + 8 files changed, 207 insertions(+) create mode 100644 cucumber/pom.xml create mode 100644 cucumber/src/main/java/com/baeldung/cucumber/calculator/Calculator.java create mode 100644 cucumber/src/test/java/com/baeldung/cucumber/calculator/CalculatorRunSteps.java create mode 100644 cucumber/src/test/java/com/baeldung/cucumber/calculator/CalculatorTest.java create mode 100644 cucumber/src/test/resources/features/calculator-scenario-outline.feature create mode 100644 cucumber/src/test/resources/features/calculator.feature create mode 100644 cucumber/src/test/resources/logback-test.xml diff --git a/cucumber/pom.xml b/cucumber/pom.xml new file mode 100644 index 0000000000..77d04f96c2 --- /dev/null +++ b/cucumber/pom.xml @@ -0,0 +1,93 @@ + + 4.0.0 + com.baeldung + cucumber + 1.0.0-SNAPSHOT + jar + + + 1.8 + 1.8 + UTF-8 + 3.5.1 + 1.7.21 + 1.1.7 + 4.12 + 1.3 + 1.2.5 + 2.19.1 + + + + + + + + info.cukes + cucumber-junit + ${cucumber.version} + test + + + + info.cukes + cucumber-java + ${cucumber.version} + test + + + + org.hamcrest + hamcrest-library + ${hamcrest.library.version} + test + + + + org.slf4j + slf4j-api + ${slf4j.version} + + + + ch.qos.logback + logback-classic + ${logback.version} + + + + ch.qos.logback + logback-core + ${logback.version} + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven.compiler.plugin.version} + + true + true + ${java.source.version} + ${java.target.version} + + + + org.apache.maven.plugins + maven-surefire-plugin + ${surefire.plugin.version} + + + **/CalculatorTest.java + + + + + + + \ No newline at end of file diff --git a/cucumber/src/main/java/com/baeldung/cucumber/calculator/Calculator.java b/cucumber/src/main/java/com/baeldung/cucumber/calculator/Calculator.java new file mode 100644 index 0000000000..fd4a3bad7b --- /dev/null +++ b/cucumber/src/main/java/com/baeldung/cucumber/calculator/Calculator.java @@ -0,0 +1,7 @@ +package com.baeldung.cucumber.calculator; + +public class Calculator { + public int add(int a, int b) { + return a + b; + } +} diff --git a/cucumber/src/test/java/com/baeldung/cucumber/calculator/CalculatorRunSteps.java b/cucumber/src/test/java/com/baeldung/cucumber/calculator/CalculatorRunSteps.java new file mode 100644 index 0000000000..9c0e920a8d --- /dev/null +++ b/cucumber/src/test/java/com/baeldung/cucumber/calculator/CalculatorRunSteps.java @@ -0,0 +1,38 @@ +package com.baeldung.cucumber.calculator; + +import org.hamcrest.Matchers; +import org.junit.Assert; + +import cucumber.api.java.Before; +import cucumber.api.java.en.Given; +import cucumber.api.java.en.Then; +import cucumber.api.java.en.When; + +public class CalculatorRunSteps { + + private int total; + + private Calculator calculator; + + @Before + private void init() { + total = -999; + + } + + @Given("^I have a calculator$") + public void initializeCalculator() throws Throwable { + calculator = new Calculator(); + } + + @When("^I add (-?\\d+) and (-?\\d+)$") + public void testAdd(int num1, int num2) throws Throwable { + total = calculator.add(num1, num2); + } + + @Then("^the result should be (-?\\d+)$") + public void validateResult(int result) throws Throwable { + Assert.assertThat(total, Matchers.equalTo(result)); + } + +} diff --git a/cucumber/src/test/java/com/baeldung/cucumber/calculator/CalculatorTest.java b/cucumber/src/test/java/com/baeldung/cucumber/calculator/CalculatorTest.java new file mode 100644 index 0000000000..6bbbca60d2 --- /dev/null +++ b/cucumber/src/test/java/com/baeldung/cucumber/calculator/CalculatorTest.java @@ -0,0 +1,15 @@ +package com.baeldung.cucumber.calculator; + +import org.junit.runner.RunWith; + +import cucumber.api.CucumberOptions; +import cucumber.api.junit.Cucumber; + +@RunWith(Cucumber.class) +@CucumberOptions( + features={"classpath:features/calculator.feature", "classpath:features/calculator-scenario-outline.feature"} + , plugin = { "pretty", "json:target/reports/json/calculator.json" } + , glue = {"com.baeldung.cucumber.calculator"} +) +public class CalculatorTest { +} diff --git a/cucumber/src/test/resources/features/calculator-scenario-outline.feature b/cucumber/src/test/resources/features/calculator-scenario-outline.feature new file mode 100644 index 0000000000..7437dbf5f9 --- /dev/null +++ b/cucumber/src/test/resources/features/calculator-scenario-outline.feature @@ -0,0 +1,16 @@ +Feature: Calculator + As a user + I want to use a calculator to add numbers + So that I don't need to add myself + + Scenario Outline: Add two numbers & + Given I have a calculator + When I add and + Then the result should be + + Examples: + | num1 | num2 | total | + | -2 | 3 | 1 | + | 10 | 15 | 25 | + | 99 | -99 | 0 | + | -1 | -10 | -11 | \ No newline at end of file diff --git a/cucumber/src/test/resources/features/calculator.feature b/cucumber/src/test/resources/features/calculator.feature new file mode 100644 index 0000000000..eaf05cb6ca --- /dev/null +++ b/cucumber/src/test/resources/features/calculator.feature @@ -0,0 +1,24 @@ +Feature: Calculator + As a user + I want to use a calculator to add numbers + So that I don't need to add myself + + Scenario: Add two numbers -2 & 3 + Given I have a calculator + When I add -2 and 3 + Then the result should be 1 + + Scenario: Add two numbers 10 & 15 + Given I have a calculator + When I add 10 and 15 + Then the result should be 25 + + Scenario: Add two numbers 99 & -99 + Given I have a calculator + When I add 99 and -99 + Then the result should be 0 + + Scenario: Add two numbers -1 & -10 + Given I have a calculator + When I add -1 and -10 + Then the result should be -11 \ No newline at end of file diff --git a/cucumber/src/test/resources/logback-test.xml b/cucumber/src/test/resources/logback-test.xml new file mode 100644 index 0000000000..e980d88693 --- /dev/null +++ b/cucumber/src/test/resources/logback-test.xml @@ -0,0 +1,13 @@ + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n%ex + + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index a582ff3e70..0b569b49fd 100644 --- a/pom.xml +++ b/pom.xml @@ -215,6 +215,7 @@ rabbitmq vertx spring-data-gemfire + cucumber