Skip to content

Commit 9fe2666

Browse files
Heidi Hesslaeubi
authored andcommitted
DataTable Example
1 parent dcb9e46 commit 9fe2666

File tree

8 files changed

+313
-0
lines changed

8 files changed

+313
-0
lines changed

examples/java-datatable/.classpath

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
10+
<attributes>
11+
<attribute name="maven.pomderived" value="true"/>
12+
<attribute name="optional" value="true"/>
13+
</attributes>
14+
</classpathentry>
15+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
16+
<attributes>
17+
<attribute name="test" value="true"/>
18+
<attribute name="optional" value="true"/>
19+
<attribute name="maven.pomderived" value="true"/>
20+
</attributes>
21+
</classpathentry>
22+
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
23+
<attributes>
24+
<attribute name="test" value="true"/>
25+
<attribute name="maven.pomderived" value="true"/>
26+
<attribute name="optional" value="true"/>
27+
</attributes>
28+
</classpathentry>
29+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
30+
<attributes>
31+
<attribute name="maven.pomderived" value="true"/>
32+
</attributes>
33+
</classpathentry>
34+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
35+
<classpathentry kind="output" path="target/classes"/>
36+
</classpath>

examples/java-datatable/.project

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>datatable-java</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22+
</natures>
23+
</projectDescription>

examples/java-datatable/pom.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>io.cucumber.eclipse.examples</groupId>
4+
<artifactId>datatable-java</artifactId>
5+
<version>0.0.1-SNAPSHOT</version>
6+
<name>Examples: Datatable</name>
7+
8+
9+
<properties>
10+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
11+
<maven.compiler.source>11</maven.compiler.source>
12+
<maven.compiler.target>11</maven.compiler.target>
13+
<cucumber-version>6.9.1</cucumber-version>
14+
<hamcrest.version>2.2</hamcrest.version>
15+
</properties>
16+
17+
18+
<dependencies>
19+
20+
<dependency>
21+
<groupId>io.cucumber</groupId>
22+
<artifactId>cucumber-java</artifactId>
23+
<version>${cucumber-version}</version>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>org.hamcrest</groupId>
28+
<artifactId>hamcrest-core</artifactId>
29+
<version>${hamcrest.version}</version>
30+
<scope>test</scope>
31+
</dependency>
32+
33+
</dependencies>
34+
</project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package cucumber.examples.datatable;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class Animals {
7+
8+
protected List<String> availableData;
9+
private List<String> availableDataForAnimals = Arrays.asList("Color","Lifespan","Whiskers","Trunk","Tusk");
10+
private String food;
11+
12+
public Animals() {
13+
super();
14+
}
15+
16+
public List<String> getAvailableData() {
17+
return availableData;
18+
}
19+
20+
public List<String> getAvailableDataForAnimals() {
21+
return availableDataForAnimals;
22+
}
23+
24+
public String getFood() {
25+
return food;
26+
}
27+
28+
public void setFood(String food) {
29+
this.food = food;
30+
}
31+
32+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package cucumber.examples.datatable;
2+
3+
import java.util.Arrays;
4+
5+
public class Cat extends Animals{
6+
7+
private String color;
8+
private String lifespan;
9+
private String whiskers;
10+
11+
public Cat() {
12+
super();
13+
availableData = Arrays.asList("Color","Lifespan","Whiskers");
14+
}
15+
16+
17+
public String getColor() {
18+
return color;
19+
}
20+
public String getLifespan() {
21+
return lifespan;
22+
}
23+
24+
public String getWhiskers() {
25+
return whiskers;
26+
}
27+
28+
public void setColor(String color) {
29+
this.color = color;
30+
}
31+
32+
public void setLifespan(String lifespan) {
33+
this.lifespan = lifespan;
34+
}
35+
36+
public void setWhiskers(String whiskers) {
37+
this.whiskers = whiskers;
38+
}
39+
40+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package cucumber.examples.datatable;
2+
3+
import java.util.Arrays;
4+
5+
public class Elephant extends Animals {
6+
7+
private String color;
8+
private String lifespan;
9+
private String trunk;
10+
private String tusk;
11+
12+
public Elephant(){
13+
super();
14+
availableData = Arrays.asList("Color","Lifespan","Trunk","Tusk");
15+
}
16+
17+
public String getColor() {
18+
return color;
19+
}
20+
21+
public String getLifespan() {
22+
return lifespan;
23+
}
24+
25+
public String getTrunk() {
26+
return trunk;
27+
}
28+
29+
public String getTusk() {
30+
return tusk;
31+
}
32+
33+
public void setColor(String color) {
34+
this.color = color;
35+
}
36+
37+
public void setLifespan(String lifespan) {
38+
this.lifespan = lifespan;
39+
}
40+
41+
public void setTrunk(String trunk) {
42+
this.trunk = trunk;
43+
}
44+
45+
public void setTusk(String tusk) {
46+
this.tusk = tusk;
47+
}
48+
49+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package cucumber.examples;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.equalTo;
5+
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
import cucumber.examples.datatable.Animals;
10+
import cucumber.examples.datatable.Cat;
11+
import cucumber.examples.datatable.Elephant;
12+
import io.cucumber.datatable.DataTable;
13+
import io.cucumber.java.en.Given;
14+
import io.cucumber.java.en.Then;
15+
16+
public class DatatableSteps {
17+
18+
Animals animal;
19+
20+
@Given("the animal {string}")
21+
public void animalCharacteristics(final String animalToSet, final DataTable tableOfParameters){
22+
List<Map<String, String>> animalData = tableOfParameters.asMaps(String.class, String.class);
23+
24+
switch (animalToSet) {
25+
case "Cat" -> {
26+
animal = new Cat();
27+
animal.setFood("fish");
28+
}
29+
case "Elephant" -> {
30+
animal = new Elephant();
31+
animal.setFood("leaves");
32+
}
33+
default -> new NoClassDefFoundError("The animal '"+ animalToSet+"' doesn't exist");
34+
}
35+
checkAnimalData(animalData);
36+
}
37+
38+
private void checkAnimalData(final List<Map<String, String>> animalData) {
39+
40+
for (Map<String, String> data : animalData) {
41+
String key = data.get("Key");
42+
assertThat("Key '" + key + "' isn't contain in the available data of all animals",animal.getAvailableDataForAnimals().contains(key));
43+
assertThat("Key '" + key + "' isn't contain in the available data of '" + animal.getClass().getSimpleName()+"'",animal.getAvailableData().contains(key));
44+
}
45+
46+
}
47+
48+
@Then("the food is {string}")
49+
public void theFoodIs(final String food) {
50+
assertThat(animal.getFood(),equalTo(food));
51+
}
52+
53+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#language: en
2+
Feature: Connection between DataTable Key and a specific Step Value
3+
4+
5+
Scenario: All Keys are related to a Step Value. Example 1
6+
Given the animal "Cat"
7+
| Key | Value |
8+
| Color | Black |
9+
| Lifespan | 3 |
10+
| Whiskers | 24 |
11+
12+
Then the food is "fish"
13+
14+
15+
Scenario: All Keys are related to a Step Value. Example 2
16+
Given the animal "Elephant"
17+
| Key | Value |
18+
| Color | Grey |
19+
| Lifespan | 70 |
20+
| Trunk | 1.8 |
21+
| Tusk | 1.3 |
22+
23+
Then the food is "leaves"
24+
25+
26+
Scenario: There are some unrelated Keys to a Step Value. This Keys are available for other Step Value
27+
Given the animal "Cat"
28+
| Key | Value |
29+
| Color | Black |
30+
| Lifespan | 3 |
31+
| Whiskers | 24 |
32+
| Trunk | 1.8 |
33+
34+
Then the food is "fish"
35+
36+
37+
Scenario: There are some unrelated Keys to a Step Value. This Keys are not available for each Step Value
38+
Given the animal "Cat"
39+
| Key | Value |
40+
| Color | Black |
41+
| Lifespan | 3 |
42+
| Whiskers | 24 |
43+
| Wings | 2 |
44+
45+
Then the food is "fish"
46+

0 commit comments

Comments
 (0)