Skip to content

Commit fd8e872

Browse files
him-bharmaibin
authored andcommitted
[BAEL-752] Cucumber with Scenario Outline (eugenp#1583)
* [BAEL-752] Cucumbr with Scenario Outline * Corrected indentation * Applied more formatting guidelines. * Java standard used for method names
1 parent ad0cd72 commit fd8e872

8 files changed

Lines changed: 207 additions & 0 deletions

File tree

‎cucumber/pom.xml‎

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>cucumber</artifactId>
6+
<version>1.0.0-SNAPSHOT</version>
7+
<packaging>jar</packaging>
8+
9+
<properties>
10+
<java.source.version>1.8</java.source.version>
11+
<java.target.version>1.8</java.target.version>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<maven.compiler.plugin.version>3.5.1</maven.compiler.plugin.version>
14+
<slf4j.version>1.7.21</slf4j.version>
15+
<logback.version>1.1.7</logback.version>
16+
<junit.version>4.12</junit.version>
17+
<hamcrest.library.version>1.3</hamcrest.library.version>
18+
<cucumber.version>1.2.5</cucumber.version>
19+
<surefire.plugin.version>2.19.1</surefire.plugin.version>
20+
</properties>
21+
22+
<modules>
23+
</modules>
24+
25+
<dependencies>
26+
<dependency>
27+
<groupId>info.cukes</groupId>
28+
<artifactId>cucumber-junit</artifactId>
29+
<version>${cucumber.version}</version>
30+
<scope>test</scope>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>info.cukes</groupId>
35+
<artifactId>cucumber-java</artifactId>
36+
<version>${cucumber.version}</version>
37+
<scope>test</scope>
38+
</dependency>
39+
40+
<dependency>
41+
<groupId>org.hamcrest</groupId>
42+
<artifactId>hamcrest-library</artifactId>
43+
<version>${hamcrest.library.version}</version>
44+
<scope>test</scope>
45+
</dependency>
46+
47+
<dependency>
48+
<groupId>org.slf4j</groupId>
49+
<artifactId>slf4j-api</artifactId>
50+
<version>${slf4j.version}</version>
51+
</dependency>
52+
53+
<dependency>
54+
<groupId>ch.qos.logback</groupId>
55+
<artifactId>logback-classic</artifactId>
56+
<version>${logback.version}</version>
57+
</dependency>
58+
59+
<dependency>
60+
<groupId>ch.qos.logback</groupId>
61+
<artifactId>logback-core</artifactId>
62+
<version>${logback.version}</version>
63+
</dependency>
64+
65+
</dependencies>
66+
67+
<build>
68+
<plugins>
69+
<plugin>
70+
<groupId>org.apache.maven.plugins</groupId>
71+
<artifactId>maven-compiler-plugin</artifactId>
72+
<version>${maven.compiler.plugin.version}</version>
73+
<configuration>
74+
<verbose>true</verbose>
75+
<fork>true</fork>
76+
<source>${java.source.version}</source>
77+
<target>${java.target.version}</target>
78+
</configuration>
79+
</plugin>
80+
<plugin>
81+
<groupId>org.apache.maven.plugins</groupId>
82+
<artifactId>maven-surefire-plugin</artifactId>
83+
<version>${surefire.plugin.version}</version>
84+
<configuration>
85+
<includes>
86+
<include>**/CalculatorTest.java</include>
87+
</includes>
88+
</configuration>
89+
</plugin>
90+
</plugins>
91+
</build>
92+
93+
</project>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.cucumber.calculator;
2+
3+
public class Calculator {
4+
public int add(int a, int b) {
5+
return a + b;
6+
}
7+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.cucumber.calculator;
2+
3+
import org.hamcrest.Matchers;
4+
import org.junit.Assert;
5+
6+
import cucumber.api.java.Before;
7+
import cucumber.api.java.en.Given;
8+
import cucumber.api.java.en.Then;
9+
import cucumber.api.java.en.When;
10+
11+
public class CalculatorRunSteps {
12+
13+
private int total;
14+
15+
private Calculator calculator;
16+
17+
@Before
18+
private void init() {
19+
total = -999;
20+
21+
}
22+
23+
@Given("^I have a calculator$")
24+
public void initializeCalculator() throws Throwable {
25+
calculator = new Calculator();
26+
}
27+
28+
@When("^I add (-?\\d+) and (-?\\d+)$")
29+
public void testAdd(int num1, int num2) throws Throwable {
30+
total = calculator.add(num1, num2);
31+
}
32+
33+
@Then("^the result should be (-?\\d+)$")
34+
public void validateResult(int result) throws Throwable {
35+
Assert.assertThat(total, Matchers.equalTo(result));
36+
}
37+
38+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.cucumber.calculator;
2+
3+
import org.junit.runner.RunWith;
4+
5+
import cucumber.api.CucumberOptions;
6+
import cucumber.api.junit.Cucumber;
7+
8+
@RunWith(Cucumber.class)
9+
@CucumberOptions(
10+
features={"classpath:features/calculator.feature", "classpath:features/calculator-scenario-outline.feature"}
11+
, plugin = { "pretty", "json:target/reports/json/calculator.json" }
12+
, glue = {"com.baeldung.cucumber.calculator"}
13+
)
14+
public class CalculatorTest {
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Feature: Calculator
2+
As a user
3+
I want to use a calculator to add numbers
4+
So that I don't need to add myself
5+
6+
Scenario Outline: Add two numbers <num1> & <num2>
7+
Given I have a calculator
8+
When I add <num1> and <num2>
9+
Then the result should be <total>
10+
11+
Examples:
12+
| num1 | num2 | total |
13+
| -2 | 3 | 1 |
14+
| 10 | 15 | 25 |
15+
| 99 | -99 | 0 |
16+
| -1 | -10 | -11 |
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Feature: Calculator
2+
As a user
3+
I want to use a calculator to add numbers
4+
So that I don't need to add myself
5+
6+
Scenario: Add two numbers -2 & 3
7+
Given I have a calculator
8+
When I add -2 and 3
9+
Then the result should be 1
10+
11+
Scenario: Add two numbers 10 & 15
12+
Given I have a calculator
13+
When I add 10 and 15
14+
Then the result should be 25
15+
16+
Scenario: Add two numbers 99 & -99
17+
Given I have a calculator
18+
When I add 99 and -99
19+
Then the result should be 0
20+
21+
Scenario: Add two numbers -1 & -10
22+
Given I have a calculator
23+
When I add -1 and -10
24+
Then the result should be -11
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<configuration scan="true">
2+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
3+
<encoder>
4+
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n%ex
5+
</pattern>
6+
</encoder>
7+
</appender>
8+
9+
<root level="INFO">
10+
<appender-ref ref="STDOUT" />
11+
</root>
12+
13+
</configuration>

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@
215215
<module>rabbitmq</module>
216216
<module>vertx</module>
217217
<module>spring-data-gemfire</module>
218+
<module>cucumber</module>
218219
</modules>
219220

220221
<build>

0 commit comments

Comments
 (0)