Skip to content

Commit 715ff92

Browse files
jit3pammpkorstanje
andauthored
Add Cucumber Kotlin Junit5 example
Co-authored-by: M.P. Korstanje <[email protected]>
1 parent 54a0fdb commit 715ff92

File tree

19 files changed

+372
-28
lines changed

19 files changed

+372
-28
lines changed

examples/calculator-java-cli/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<artifactId>calculator-java-cli</artifactId>
1111
<packaging>jar</packaging>
12-
<name>Examples: Calculator - Annotations - CLI</name>
12+
<name>Examples: Calculator - Java - Annotations - CLI</name>
1313

1414
<properties>
1515
<project.Automatic-Module-Name>io.cucumber.examples.calculator</project.Automatic-Module-Name>

examples/calculator-java-junit4/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<artifactId>calculator-java-junit4</artifactId>
1111
<packaging>jar</packaging>
12-
<name>Examples: Calculator - Annotations - Junit4</name>
12+
<name>Examples: Calculator - Java - Annotations - Junit4</name>
1313

1414
<properties>
1515
<project.Automatic-Module-Name>io.cucumber.examples.calculator</project.Automatic-Module-Name>

examples/calculator-java-junit5/pom.xml

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
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">
23
<modelVersion>4.0.0</modelVersion>
34

45
<parent>
@@ -9,10 +10,10 @@
910

1011
<artifactId>calculator-java-junit5</artifactId>
1112
<packaging>jar</packaging>
12-
<name>Examples: Calculator - Annotations - Junit 5</name>
13+
<name>Examples: Calculator - Java - Annotations - Junit 5</name>
1314

1415
<properties>
15-
<project.Automatic-Module-Name>io.cucumber.calculator</project.Automatic-Module-Name>
16+
<project.Automatic-Module-Name>io.cucumber.examples.calculator</project.Automatic-Module-Name>
1617
</properties>
1718

1819
<dependencyManagement>
@@ -27,7 +28,7 @@
2728
<dependency>
2829
<groupId>org.junit</groupId>
2930
<artifactId>junit-bom</artifactId>
30-
<version>5.10.1</version>
31+
<version>5.12.2</version>
3132
<type>pom</type>
3233
<scope>import</scope>
3334
</dependency>
@@ -38,6 +39,13 @@
3839
<type>pom</type>
3940
<scope>import</scope>
4041
</dependency>
42+
<dependency>
43+
<groupId>org.assertj</groupId>
44+
<artifactId>assertj-bom</artifactId>
45+
<version>3.27.3</version>
46+
<type>pom</type>
47+
<scope>import</scope>
48+
</dependency>
4149
</dependencies>
4250
</dependencyManagement>
4351

@@ -57,11 +65,6 @@
5765
<artifactId>junit-platform-suite</artifactId>
5866
<scope>test</scope>
5967
</dependency>
60-
<dependency>
61-
<groupId>org.junit.jupiter</groupId>
62-
<artifactId>junit-jupiter-api</artifactId>
63-
<scope>test</scope>
64-
</dependency>
6568
<dependency>
6669
<groupId>org.junit.platform</groupId>
6770
<artifactId>junit-platform-console</artifactId>
@@ -72,6 +75,11 @@
7275
<artifactId>jackson-databind</artifactId>
7376
<scope>test</scope>
7477
</dependency>
78+
<dependency>
79+
<groupId>org.assertj</groupId>
80+
<artifactId>assertj-core</artifactId>
81+
<scope>test</scope>
82+
</dependency>
7583
</dependencies>
7684

7785
<build>
@@ -106,14 +114,16 @@
106114
</goals>
107115
<configuration>
108116
<target unless="maven.test.skip">
109-
<echo message="Running JUnit Platform CLI" />
110-
<java classname="org.junit.platform.console.ConsoleLauncher" fork="true" failonerror="true" newenvironment="true" maxmemory="512m" classpathref="maven.test.classpath">
111-
<arg value="--include-engine" />
112-
<arg value="cucumber" />
113-
<arg value="--scan-classpath" />
114-
<arg value="${project.build.testOutputDirectory}" />
115-
<arg value="--reports-dir" />
116-
<arg value="${project.build.directory}/cucumber-reports" />
117+
<echo message="Running JUnit Platform CLI"/>
118+
<java classname="org.junit.platform.console.ConsoleLauncher" fork="true"
119+
failonerror="true" newenvironment="true" maxmemory="512m"
120+
classpathref="maven.test.classpath">
121+
<arg value="--include-engine"/>
122+
<arg value="cucumber"/>
123+
<arg value="--scan-classpath"/>
124+
<arg value="${project.build.testOutputDirectory}"/>
125+
<arg value="--reports-dir"/>
126+
<arg value="${project.build.directory}/cucumber-reports"/>
117127
</java>
118128
</target>
119129
</configuration>

examples/calculator-java-junit5/src/test/java/io/cucumber/examples/calculator/DateStepDefinitions.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import java.util.Date;
88

9-
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
import static org.assertj.core.api.Assertions.assertThat;
1010

1111
public class DateStepDefinitions {
1212

@@ -25,6 +25,6 @@ public void I_ask_if_date_is_in_the_past(Date date) {
2525

2626
@Then("^the result should be (yes|no)$")
2727
public void the_result_should_be(String expectedResult) {
28-
assertEquals(expectedResult, result);
28+
assertThat(result).isEqualTo(expectedResult);
2929
}
3030
}

examples/calculator-java-junit5/src/test/java/io/cucumber/examples/calculator/RpnCalculatorSteps.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import java.util.List;
1313

14-
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
import static org.assertj.core.api.Assertions.assertThat;
1515

1616
public class RpnCalculatorSteps {
1717

@@ -66,7 +66,7 @@ public void I_press(String what) {
6666

6767
@Then("the result is {int}")
6868
public void the_result_is(double expected) {
69-
assertEquals(expected, calc.value());
69+
assertThat(calc.value()).isEqualTo(expected);
7070
}
7171

7272
@Given("the previous entries:")

examples/calculator-java-junit5/src/test/java/io/cucumber/examples/calculator/ShoppingSteps.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import java.util.List;
88

9-
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
import static org.assertj.core.api.Assertions.assertThat;
1010

1111
public class ShoppingSteps {
1212

@@ -28,7 +28,7 @@ public void i_pay(int amount) {
2828

2929
@Then("my change should be {}")
3030
public void my_change_should_be_(int change) {
31-
assertEquals(-calc.value().intValue(), change);
31+
assertThat(-calc.value().intValue()).isEqualTo(change);
3232
}
3333

3434
static class Grocery {

examples/calculator-java-testng/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<artifactId>calculator-java-testng</artifactId>
1111
<packaging>jar</packaging>
12-
<name>Examples: Calculator - Annotations - TestNG</name>
12+
<name>Examples: Calculator - Java - Annotations - TestNG</name>
1313

1414
<properties>
1515
<project.Automatic-Module-Name>io.cucumber.examples.calculator</project.Automatic-Module-Name>

examples/calculator-java8-cli/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<artifactId>calculator-java8-cli</artifactId>
1111
<packaging>jar</packaging>
12-
<name>Examples: Calculator - Lambda - CLI</name>
12+
<name>Examples: Calculator - Java - Lambda - CLI</name>
1313

1414
<properties>
1515
<project.Automatic-Module-Name>io.cucumber.examples.calculator</project.Automatic-Module-Name>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/.settings
2+
/.classpath
3+
/.project
4+
/test-output/
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<parent>
8+
<groupId>io.cucumber</groupId>
9+
<artifactId>examples</artifactId>
10+
<version>7.23.0-SNAPSHOT</version>
11+
</parent>
12+
13+
<artifactId>calculator-kotlin-junit5</artifactId>
14+
<packaging>jar</packaging>
15+
<name>Examples: Calculator - Kotlin - Annotations - Junit 5</name>
16+
17+
<properties>
18+
<kotlin.code.style>official</kotlin.code.style>
19+
<project.Automatic-Module-Name>io.cucumber.examples.calculator</project.Automatic-Module-Name>
20+
</properties>
21+
22+
<dependencyManagement>
23+
<dependencies>
24+
<dependency>
25+
<groupId>io.cucumber</groupId>
26+
<artifactId>cucumber-bom</artifactId>
27+
<version>${project.version}</version>
28+
<type>pom</type>
29+
<scope>import</scope>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.junit</groupId>
33+
<artifactId>junit-bom</artifactId>
34+
<version>5.12.2</version>
35+
<type>pom</type>
36+
<scope>import</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.jetbrains.kotlin</groupId>
40+
<artifactId>kotlin-bom</artifactId>
41+
<version>2.1.21</version>
42+
<type>pom</type>
43+
<scope>import</scope>
44+
</dependency>
45+
</dependencies>
46+
</dependencyManagement>
47+
48+
<dependencies>
49+
<dependency>
50+
<groupId>org.jetbrains.kotlin</groupId>
51+
<artifactId>kotlin-stdlib</artifactId>
52+
</dependency>
53+
<dependency>
54+
<groupId>io.cucumber</groupId>
55+
<artifactId>cucumber-java</artifactId>
56+
<scope>test</scope>
57+
</dependency>
58+
<dependency>
59+
<groupId>io.cucumber</groupId>
60+
<artifactId>cucumber-junit-platform-engine</artifactId>
61+
<scope>test</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>org.junit.platform</groupId>
65+
<artifactId>junit-platform-suite</artifactId>
66+
<scope>test</scope>
67+
</dependency>
68+
<dependency>
69+
<groupId>org.junit.platform</groupId>
70+
<artifactId>junit-platform-console</artifactId>
71+
<scope>test</scope>
72+
</dependency>
73+
<dependency>
74+
<groupId>org.jetbrains.kotlin</groupId>
75+
<artifactId>kotlin-test</artifactId>
76+
</dependency>
77+
</dependencies>
78+
79+
<build>
80+
<sourceDirectory>src/main/kotlin</sourceDirectory>
81+
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
82+
83+
<plugins>
84+
<plugin>
85+
<groupId>org.jetbrains.kotlin</groupId>
86+
<artifactId>kotlin-maven-plugin</artifactId>
87+
<version>2.1.21</version>
88+
<executions>
89+
<execution>
90+
<id>compile</id>
91+
<phase>compile</phase>
92+
<goals>
93+
<goal>compile</goal>
94+
</goals>
95+
</execution>
96+
<execution>
97+
<id>test-compile</id>
98+
<phase>test-compile</phase>
99+
<goals>
100+
<goal>test-compile</goal>
101+
</goals>
102+
</execution>
103+
</executions>
104+
</plugin>
105+
<plugin>
106+
<artifactId>maven-failsafe-plugin</artifactId>
107+
<version>3.5.3</version>
108+
</plugin>
109+
<plugin>
110+
<groupId>org.apache.maven.plugins</groupId>
111+
<artifactId>maven-surefire-plugin</artifactId>
112+
<configuration>
113+
<properties>
114+
<!-- Work around. Surefire does not include enough information to disambiguate between different
115+
examples. -->
116+
<configurationParameters>
117+
cucumber.junit-platform.naming-strategy=long
118+
</configurationParameters>
119+
</properties>
120+
</configuration>
121+
</plugin>
122+
123+
<plugin>
124+
<groupId>org.apache.maven.plugins</groupId>
125+
<artifactId>maven-antrun-plugin</artifactId>
126+
<executions>
127+
<execution>
128+
<!-- Work around. Surefire does not use JUnits Test Engine discovery functionality.
129+
130+
Alternatively use the JUnit Platform Suite Engine.
131+
-->
132+
<id>cli-test</id>
133+
<phase>test</phase>
134+
<goals>
135+
<goal>run</goal>
136+
</goals>
137+
<configuration>
138+
<target unless="maven.test.skip">
139+
<echo message="Running JUnit Platform CLI"/>
140+
<java classname="org.junit.platform.console.ConsoleLauncher" fork="true"
141+
failonerror="true" newenvironment="true" maxmemory="512m"
142+
classpathref="maven.test.classpath">
143+
<arg value="--include-engine"/>
144+
<arg value="cucumber"/>
145+
<arg value="--scan-classpath"/>
146+
<arg value="${project.build.testOutputDirectory}"/>
147+
<arg value="--reports-dir"/>
148+
<arg value="${project.build.directory}/cucumber-reports"/>
149+
</java>
150+
</target>
151+
</configuration>
152+
</execution>
153+
</executions>
154+
</plugin>
155+
</plugins>
156+
</build>
157+
158+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.cucumber.examples.calaculator
2+
3+
import java.time.LocalDate
4+
import java.util.*
5+
6+
class DateCalculator(private val now: LocalDate) {
7+
8+
fun isDateInThePast(date: LocalDate): String {
9+
return if (date.isBefore(now)) "yes" else "no"
10+
}
11+
}
12+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.cucumber.examples.calaculator
2+
3+
class RpnCalculator {
4+
private val stack: ArrayDeque<Number> = ArrayDeque()
5+
6+
private val OPS = setOf("+", "-", "*", "/")
7+
8+
fun push(arg: Any) {
9+
if (arg in OPS) {
10+
val y = stack.removeLast()
11+
val x = if (stack.isEmpty()) 0 else stack.removeLast()
12+
val valResult = when (arg) {
13+
"-" -> x.toDouble() - y.toDouble()
14+
"+" -> x.toDouble() + y.toDouble()
15+
"*" -> x.toDouble() * y.toDouble()
16+
"/" -> x.toDouble() / y.toDouble()
17+
else -> throw IllegalArgumentException("Unknown operation $arg")
18+
}
19+
push(valResult)
20+
} else {
21+
stack.addLast(arg as Number)
22+
}
23+
}
24+
25+
fun peek(): Number? = stack.lastOrNull()
26+
27+
fun clear() = stack.clear()
28+
}
29+

0 commit comments

Comments
 (0)