Skip to content

Commit

Permalink
DataTable Example
Browse files Browse the repository at this point in the history
  • Loading branch information
Heidi Hess authored and laeubi committed Oct 23, 2023
1 parent dcb9e46 commit 9fe2666
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 0 deletions.
36 changes: 36 additions & 0 deletions examples/java-datatable/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="test" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
23 changes: 23 additions & 0 deletions examples/java-datatable/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>datatable-java</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
34 changes: 34 additions & 0 deletions examples/java-datatable/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>io.cucumber.eclipse.examples</groupId>
<artifactId>datatable-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Examples: Datatable</name>


<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<cucumber-version>6.9.1</cucumber-version>
<hamcrest.version>2.2</hamcrest.version>
</properties>


<dependencies>

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber-version}</version>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>${hamcrest.version}</version>
<scope>test</scope>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cucumber.examples.datatable;

import java.util.Arrays;
import java.util.List;

public class Animals {

protected List<String> availableData;
private List<String> availableDataForAnimals = Arrays.asList("Color","Lifespan","Whiskers","Trunk","Tusk");
private String food;

public Animals() {
super();
}

public List<String> getAvailableData() {
return availableData;
}

public List<String> getAvailableDataForAnimals() {
return availableDataForAnimals;
}

public String getFood() {
return food;
}

public void setFood(String food) {
this.food = food;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package cucumber.examples.datatable;

import java.util.Arrays;

public class Cat extends Animals{

private String color;
private String lifespan;
private String whiskers;

public Cat() {
super();
availableData = Arrays.asList("Color","Lifespan","Whiskers");
}


public String getColor() {
return color;
}
public String getLifespan() {
return lifespan;
}

public String getWhiskers() {
return whiskers;
}

public void setColor(String color) {
this.color = color;
}

public void setLifespan(String lifespan) {
this.lifespan = lifespan;
}

public void setWhiskers(String whiskers) {
this.whiskers = whiskers;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package cucumber.examples.datatable;

import java.util.Arrays;

public class Elephant extends Animals {

private String color;
private String lifespan;
private String trunk;
private String tusk;

public Elephant(){
super();
availableData = Arrays.asList("Color","Lifespan","Trunk","Tusk");
}

public String getColor() {
return color;
}

public String getLifespan() {
return lifespan;
}

public String getTrunk() {
return trunk;
}

public String getTusk() {
return tusk;
}

public void setColor(String color) {
this.color = color;
}

public void setLifespan(String lifespan) {
this.lifespan = lifespan;
}

public void setTrunk(String trunk) {
this.trunk = trunk;
}

public void setTusk(String tusk) {
this.tusk = tusk;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package cucumber.examples;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

import java.util.List;
import java.util.Map;

import cucumber.examples.datatable.Animals;
import cucumber.examples.datatable.Cat;
import cucumber.examples.datatable.Elephant;
import io.cucumber.datatable.DataTable;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;

public class DatatableSteps {

Animals animal;

@Given("the animal {string}")
public void animalCharacteristics(final String animalToSet, final DataTable tableOfParameters){
List<Map<String, String>> animalData = tableOfParameters.asMaps(String.class, String.class);

switch (animalToSet) {
case "Cat" -> {
animal = new Cat();
animal.setFood("fish");
}
case "Elephant" -> {
animal = new Elephant();
animal.setFood("leaves");
}
default -> new NoClassDefFoundError("The animal '"+ animalToSet+"' doesn't exist");
}
checkAnimalData(animalData);
}

private void checkAnimalData(final List<Map<String, String>> animalData) {

for (Map<String, String> data : animalData) {
String key = data.get("Key");
assertThat("Key '" + key + "' isn't contain in the available data of all animals",animal.getAvailableDataForAnimals().contains(key));
assertThat("Key '" + key + "' isn't contain in the available data of '" + animal.getClass().getSimpleName()+"'",animal.getAvailableData().contains(key));
}

}

@Then("the food is {string}")
public void theFoodIs(final String food) {
assertThat(animal.getFood(),equalTo(food));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#language: en
Feature: Connection between DataTable Key and a specific Step Value


Scenario: All Keys are related to a Step Value. Example 1
Given the animal "Cat"
| Key | Value |
| Color | Black |
| Lifespan | 3 |
| Whiskers | 24 |

Then the food is "fish"


Scenario: All Keys are related to a Step Value. Example 2
Given the animal "Elephant"
| Key | Value |
| Color | Grey |
| Lifespan | 70 |
| Trunk | 1.8 |
| Tusk | 1.3 |

Then the food is "leaves"


Scenario: There are some unrelated Keys to a Step Value. This Keys are available for other Step Value
Given the animal "Cat"
| Key | Value |
| Color | Black |
| Lifespan | 3 |
| Whiskers | 24 |
| Trunk | 1.8 |

Then the food is "fish"


Scenario: There are some unrelated Keys to a Step Value. This Keys are not available for each Step Value
Given the animal "Cat"
| Key | Value |
| Color | Black |
| Lifespan | 3 |
| Whiskers | 24 |
| Wings | 2 |

Then the food is "fish"

0 comments on commit 9fe2666

Please sign in to comment.