Skip to content

Instantly share code, notes, and snippets.

@jwgmeligmeyling
Created May 5, 2016 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jwgmeligmeyling/d22656e91e3851dc4ed0fcbd92da15ff to your computer and use it in GitHub Desktop.
Save jwgmeligmeyling/d22656e91e3851dc4ed0fcbd92da15ff to your computer and use it in GitHub Desktop.

Cucumber Example

Example for the Software Quality and Testing course on how to easily separate step definitions across different step definiton classes, and by that means improve reuability of the step definitions.

Feature: 1. Simple number arithmetic
As a user
I want to be able to do simple number arithmetic
So that I can answer my exam questions
Scenario: 1. Add two numbers
Given the numbers 3 and 4
When I add the two numbers
Then the result should be 7
Scenario: 2. Subtract two numbers
Given the numbers 2 and 1
When I subtract the two numbers
Then the result should be 1
package nl.tudelft.cucumber;
import java.io.Closeable;
public class Calculator implements Closeable {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a -b;
}
@Override
public void close() {
// Some hypothetical close function
}
}
package nl.tudelft.cucumber;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.equalTo;
public class CalculatorStepDefinitions {
private Calculator calculator;
private int result;
@Before
public void setup() {
calculator = new Calculator();
}
@When("^I add the two numbers$")
public void iAddTheTwoNumbers() throws Throwable {
result = calculator.add(NumberStepDefinitions.A, NumberStepDefinitions.B);
}
@Then("^the result should be (\\d+)$")
public void theResultShouldBe(int expectedResult) throws Throwable {
assertThat(result, equalTo(expectedResult));
}
@When("^I subtract the two numbers$")
public void iSubtractTheTwoNumbers() throws Throwable {
result = calculator.subtract(NumberStepDefinitions.A, NumberStepDefinitions.B);
}
@After
public void tearDown() {
calculator.close();
}
}
package nl.tudelft.cucumber;
import cucumber.api.CucumberOptions;
import cucumber.api.SnippetType;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(
plugin = {"pretty"},
snippets = SnippetType.CAMELCASE,
features = "src/test/resources/features")
public class CucumberTest {
/**
* This class should be empty, step definitions should be in separate classes.
*/
}
package nl.tudelft.cucumber;
import cucumber.api.java.en.Given;
public class NumberStepDefinitions {
public static int A, B;
@Given("^the numbers (\\d+) and (\\d+)$")
public void theNumbersAnd(int first, int second) throws Throwable {
A = first;
B = second;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<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>cucumber.example</groupId>
<artifactId>example</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<junit.version>4.12</junit.version>
<mockito.version>2.0.47-beta</mockito.version>
<hamcrest.version>1.3</hamcrest.version>
<cucumber.version>1.2.4</cucumber.version>
</properties>
<developers>
<developer>
<id>jgmeligmeyling</id>
<name>Jan-Willem Gmelig Meyling</name>
<email>j.gmeligmeyling@student.tudelft.nl</email>
</developer>
</developers>
<organization>
<name>TU Delft</name>
<url>http://www.tudelft.nl/</url>
</organization>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>${hamcrest.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment