-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Add Cucumber Kotlin Junit5 example #2999
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mpkorstanje
merged 30 commits into
cucumber:main
from
jit3pam:cucumber-kotlin-testng-examples
May 23, 2025
Merged
Changes from 8 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
271b1a3
first commit
jit3pam 8ca70be
add examples for Kotlin
jit3pam 6311992
add examples for Kotlin date_calculator.feature
jit3pam b413ea6
Merge branch 'cucumber:main' into cucumber-kotlin-testng-examples
jit3pam 2cbc758
add implementations for Kotlin date_calculator.feature
jit3pam 27d061b
remove tag from RunCucumberTest.kt
jit3pam b3216e0
Merge remote-tracking branch 'origin/cucumber-kotlin-testng-examples'…
jit3pam 5683424
Merge branch 'cucumber:main' into cucumber-kotlin-testng-examples
jit3pam 785035c
Update examples/calculator-kotlin-testng/pom.xml
jit3pam 326c3c6
restore pom.xml in examples
jit3pam 698675f
update examples to use junit5
jit3pam 50f08a4
Merge branch 'cucumber:main' into cucumber-kotlin-testng-examples
jit3pam 194ab02
update dependencies in pom.xml
jit3pam 7684f62
update dependencies in pom.xml, and change .* imports to explicit imp…
jit3pam ca7df3c
first commit
jit3pam 0136a44
add examples for Kotlin
jit3pam b0db4b4
add examples for Kotlin date_calculator.feature
jit3pam 7f46e5f
add implementations for Kotlin date_calculator.feature
jit3pam b954cfc
remove tag from RunCucumberTest.kt
jit3pam e6fba37
Update examples/calculator-kotlin-testng/pom.xml
jit3pam 8125d05
restore pom.xml in examples
jit3pam 8c1d5cf
update examples to use junit5
jit3pam 450409c
update dependencies in pom.xml
jit3pam 8390b15
update dependencies in pom.xml, and change .* imports to explicit imp…
jit3pam 0a28103
restore the xml in examples
jit3pam 6e8a56c
restore the xml in examples
jit3pam 3478532
update the tags in examples/pom.xml
jit3pam 6d15200
Merge remote-tracking branch 'origin/main' into cucumber-kotlin-testn…
mpkorstanje 1f83c0b
[All] Set version to 7.23.0-SNAPSHOT
mpkorstanje b253d5e
Harmonize
mpkorstanje File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/.settings | ||
/.classpath | ||
/.project | ||
/test-output/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<artifactId>examples</artifactId> | ||
<groupId>io.cucumber</groupId> | ||
<version>7.22.3-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>calculator-kotlin-testng</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>Kotlin Examples: Calculator - Annotations - TestNG</name> | ||
jit3pam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<kotlin.version>1.9.0</kotlin.version> | ||
<kotlin.code.style>official</kotlin.code.style> | ||
<junit.version>4.13.1</junit.version> | ||
jit3pam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.jetbrains.kotlin</groupId> | ||
<artifactId>kotlin-stdlib</artifactId> | ||
<version>${kotlin.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jetbrains.kotlin</groupId> | ||
<artifactId>kotlin-test-junit</artifactId> | ||
<version>${kotlin.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>${junit.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.cucumber</groupId> | ||
<artifactId>cucumber-java</artifactId> | ||
<version>7.22.3-SNAPSHOT</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.cucumber</groupId> | ||
<artifactId>cucumber-testng</artifactId> | ||
<version>7.22.3-SNAPSHOT</version> | ||
jit3pam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<sourceDirectory>src/main/kotlin</sourceDirectory> | ||
<testSourceDirectory>src/test/kotlin</testSourceDirectory> | ||
|
||
<plugins> | ||
<plugin> | ||
<groupId>org.jetbrains.kotlin</groupId> | ||
<artifactId>kotlin-maven-plugin</artifactId> | ||
<version>${kotlin.version}</version> | ||
<executions> | ||
<execution> | ||
<id>compile</id> | ||
<phase>compile</phase> | ||
<goals> | ||
<goal>compile</goal> | ||
</goals> | ||
</execution> | ||
<execution> | ||
<id>test-compile</id> | ||
<phase>test-compile</phase> | ||
<goals> | ||
<goal>test-compile</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
12 changes: 12 additions & 0 deletions
12
...lculator-kotlin-testng/src/main/kotlin/io/cucumber/examples/calaculator/DateCalculator.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.cucumber.examples.calaculator | ||
|
||
import java.time.LocalDate | ||
import java.util.* | ||
jit3pam marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
class DateCalculator(private val now: LocalDate) { | ||
|
||
fun isDateInThePast(date: LocalDate): String { | ||
return if (date.isBefore(now)) "yes" else "no" | ||
} | ||
} | ||
|
29 changes: 29 additions & 0 deletions
29
...alculator-kotlin-testng/src/main/kotlin/io/cucumber/examples/calaculator/RpnCalculator.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.cucumber.examples.calaculator | ||
|
||
class RpnCalculator { | ||
private val stack: ArrayDeque<Number> = ArrayDeque() | ||
|
||
private val OPS = setOf("+", "-", "*", "/") | ||
|
||
fun push(arg: Any) { | ||
if (arg in OPS) { | ||
val y = stack.removeLast() | ||
val x = if (stack.isEmpty()) 0 else stack.removeLast() | ||
val valResult = when (arg) { | ||
"-" -> x.toDouble() - y.toDouble() | ||
"+" -> x.toDouble() + y.toDouble() | ||
"*" -> x.toDouble() * y.toDouble() | ||
"/" -> x.toDouble() / y.toDouble() | ||
else -> throw IllegalArgumentException("Unknown operation $arg") | ||
} | ||
push(valResult) | ||
} else { | ||
stack.addLast(arg as Number) | ||
} | ||
} | ||
|
||
fun peek(): Number? = stack.lastOrNull() | ||
|
||
fun clear() = stack.clear() | ||
} | ||
|
36 changes: 36 additions & 0 deletions
36
...ator-kotlin-testng/src/test/kotlin/io/cucumber/examples/calculator/DateStepDefinitions.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.cucumber.examples.calculator | ||
|
||
import io.cucumber.examples.calaculator.DateCalculator | ||
import io.cucumber.java.en.Given | ||
import io.cucumber.java.en.Then | ||
import io.cucumber.java.en.When | ||
import java.time.LocalDate | ||
import java.time.format.DateTimeFormatter | ||
import java.util.* | ||
import kotlin.test.assertEquals | ||
|
||
class DateStepDefinitions { | ||
|
||
private lateinit var result: String | ||
|
||
private lateinit var calculator: DateCalculator | ||
|
||
|
||
@Given("today is {}") | ||
fun todayIs(date: String) { | ||
val date1 = LocalDate.parse(date) | ||
calculator = DateCalculator(now = date1) | ||
} | ||
|
||
@When("I ask if {} is in the past") | ||
fun iAskIfDateIsInPast(date: String) { | ||
val formatter: DateTimeFormatter = DateTimeFormatter.ofPattern("MMM d, yyyy", Locale.ENGLISH) | ||
val date2 = LocalDate.parse(date,formatter) | ||
result = calculator.isDateInThePast(date2) | ||
} | ||
|
||
@Then("the result should be {string}") | ||
fun theResultShouldBeYes(answer: String) { | ||
assertEquals(answer, result) | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...lculator-kotlin-testng/src/test/kotlin/io/cucumber/examples/calculator/RunCucumberTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.cucumber.examples.calculator | ||
|
||
import io.cucumber.testng.AbstractTestNGCucumberTests | ||
import io.cucumber.testng.CucumberOptions | ||
import org.testng.annotations.DataProvider | ||
|
||
|
||
@CucumberOptions( | ||
plugin = ["html:target/results.html"] | ||
) | ||
class RunCucumberTest : AbstractTestNGCucumberTests() { | ||
|
||
@DataProvider | ||
override fun scenarios(): Array<Array<Any>> { | ||
return super.scenarios() | ||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
...-kotlin-testng/src/test/kotlin/io/cucumber/examples/calculator/ShoppingStepDefinitions.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package io.cucumber.examples.calculator | ||
|
||
import io.cucumber.datatable.DataTable | ||
import io.cucumber.examples.calaculator.RpnCalculator | ||
import io.cucumber.java.DataTableType | ||
import io.cucumber.java.en.Given | ||
import io.cucumber.java.en.Then | ||
import io.cucumber.java.en.When | ||
import kotlin.test.assertEquals | ||
|
||
class ShoppingStepDefinitions { | ||
private val rpnCalculator = RpnCalculator() | ||
private val groceryList = mutableListOf<Grocery>() | ||
|
||
|
||
|
||
@Given("the following groceries") | ||
fun givenTheFollowingGroceries(grocery: DataTable){ | ||
val groceries: List<Grocery> = grocery.asList(Grocery::class.java) | ||
groceryList.addAll(groceries) | ||
for (gro in groceries){ | ||
rpnCalculator.push(gro.price.value) | ||
rpnCalculator.push("+") | ||
} | ||
} | ||
|
||
@When("I pay {int}") | ||
fun whenIPay(amount: Int) { | ||
rpnCalculator.push(amount) | ||
rpnCalculator.push("-") | ||
} | ||
|
||
@Then("my change should be {int}") | ||
fun myChangeShouldBe(change: Int) { | ||
rpnCalculator.peek()?.let { assertEquals(change, -it.toInt()) } | ||
} | ||
|
||
@DataTableType | ||
fun groceryEntry(entry: Map<String, String>): Grocery { | ||
val name = entry["name"] ?: error("Missing name") | ||
val price = Price.fromString(entry["price"] ?: error("Missing price")) | ||
return Grocery(name, price) | ||
} | ||
|
||
data class Grocery(val name: String, val price: Price) | ||
|
||
@JvmInline | ||
value class Price(val value: Int) { | ||
companion object { | ||
fun fromString(value: String): Price { | ||
return Price(value.toInt()) | ||
} | ||
} | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
...kotlin-testng/src/test/resources/io/cucumber/examples/calculator/basic_arithmetic.feature
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
@foo | ||
Feature: Basic Arithmetic | ||
|
||
Background: A Calculator | ||
Given a calculator I just turned on | ||
|
||
Scenario: Addition | ||
# Try to change one of the values below to provoke a failure | ||
When I add 4 and 5 | ||
Then the result is 9 | ||
|
||
Scenario: Another Addition | ||
# Try to change one of the values below to provoke a failure | ||
When I add 4 and 7 | ||
Then the result is 11 | ||
|
||
Scenario Outline: Many additions | ||
Given the previous entries: | ||
| first | second | operation | | ||
| 1 | 1 | + | | ||
| 2 | 1 | + | | ||
When I press + | ||
And I add <a> and <b> | ||
And I press + | ||
Then the result is <c> | ||
|
||
Examples: Single digits | ||
| a | b | c | | ||
| 1 | 2 | 8 | | ||
| 2 | 3 | 10 | | ||
|
||
Examples: Double digits | ||
| a | b | c | | ||
| 10 | 20 | 35 | | ||
| 20 | 30 | 55 | |
9 changes: 9 additions & 0 deletions
9
...-kotlin-testng/src/test/resources/io/cucumber/examples/calculator/date_calculator.feature
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Feature: Dates with different date formats | ||
This feature shows you can have different date formats, as long as you annotate the | ||
corresponding step definition method accordingly. | ||
|
||
@date | ||
Scenario: Determine past date | ||
Given today is 2011-01-20 | ||
When I ask if Jan 19, 2011 is in the past | ||
Then the result should be "yes" |
11 changes: 11 additions & 0 deletions
11
...culator-kotlin-testng/src/test/resources/io/cucumber/examples/calculator/shopping.feature
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Feature: Shopping | ||
|
||
@shopping | ||
Scenario: Give correct change | ||
Given the following groceries | ||
| name | price | | ||
| milk | 9 | | ||
| bread | 7 | | ||
| soap | 5 | | ||
When I pay 25 | ||
Then my change should be 4 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.