Hello Cucumber
It’s a bit difficult to try to use Cucumber following official document. So here I’m documenting my experience on setting up Cucumber together with Junit.
Frist, create a new Maven project, and then setup the dependencies:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<cucumber.version>7.34.3</cucumber.version>
<junit-platform.version>6.0.3</junit-platform.version>
<junit-jupiter.version>6.0.3</junit-jupiter.version>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<version>${junit-platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.27.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit-platform-engine</artifactId>
<version>${cucumber.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
Next, create a test runner class src/test/java/org/example/RunCucumberTest.java.1
2
3
4
5
6
7
8
9
10
11package org.example;
import org.junit.platform.suite.api.*;
public class RunCucumberTest {
}
Now we are good to create our feature file src/test/resources/features/is_it_friday_yet.feature,1
2
3
4
5
6
7
8
9
10
11
12
13Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario Outline: Today is or is not Friday
Given today is "<day>"
When I ask whether it's Friday yet
Then I should be told "<answer>"
Examples:
| day | answer |
| Friday | TGIF |
| Sunday | Nope |
| anything else ! | Nope |
And step definitions src/test/java/org/example/stepdefinitions/Stepdefs.java:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33package org.example.stepdefinitions;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
class IsItFriday {
static String isItFriday(String today) {
return "Friday".equals(today) ? "TGIF" : "Nope";
}
}
public class Stepdefs {
private String today;
private String actualAnswer;
public void today_is(String today) {
this.today = today;
}
public void i_ask_whether_it_s_Friday_yet() {
actualAnswer = IsItFriday.isItFriday(today);
}
public void i_should_be_told(String expectedAnswer) {
assertThat(actualAnswer).isEqualTo(expectedAnswer);
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33package org.example.stepdefinitions;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
class IsItFriday {
static String isItFriday(String today) {
return "Friday".equals(today) ? "TGIF" : "Nope";
}
}
public class Stepdefs {
private String today;
private String actualAnswer;
public void today_is(String today) {
this.today = today;
}
public void i_ask_whether_it_s_Friday_yet() {
actualAnswer = IsItFriday.isItFriday(today);
}
public void i_should_be_told(String expectedAnswer) {
assertThat(actualAnswer).isEqualTo(expectedAnswer);
}
}
Let’s run the test with mvnd clean test:1
2
3
4
5
6
7
8
9
10
11
12[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.424 s -- in org.example.RunCucumberTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --------------------------------------------------------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] --------------------------------------------------------------------------------------------------------------------------
[INFO] Total time: 3.345 s (Wall Clock)
[INFO] Finished at: 2026-03-09T21:52:04+08:00
[INFO] --------------------------------------------------------------------------------------------------------------------------
To run test directly within Intellij IDEA, we can install the Cucumber for Java plugin, then we’ll see a green play button next to the feature file.