I recommend you to start this article by researching BDD. It would be useful to know at least what the gherkin format is and how to extract specs with examples. I would like to share the details of these subjects with you in the coming days. But, in this article, we will go on; how to create a cucumber project using the latest versions from 0?

You want to start a project with Cucumber and Java, and you don’t know what to do and where to start. There are many repositories in Github, you can start quickly, but you can be sure that you will not use the latest versions of the imported classes. Because we will work with the current version as we do now. And this version will be obsolete at a later date.

Let me first mention the place I base on; 10-min tutorial of cucumber documentation

You can continue there, but I will be sharing the points I encountered while creating. If you start from 0, I can guarantee that it will not end in 10 minutes.

I will use IntelliJ IDEA, Maven. However, if you use another ide and Gradle that you will use, the steps will be similar. Here we start with the prerequisites;

  • Java SE
  • A build tool. You can choose between:
    • Maven – version 3.3.1 or higher (I will use Maven 3.6.3) (You should add $M2_HOME/bin to your PATH variable in order to execute mvn command)
    • Gradle
  • IntelliJ IDEA with Cucumber for Java plugin

You can use the terminal (or cmd) to create a new project with maven.

mvn archetype:generate                      \
   "-DarchetypeGroupId=io.cucumber"           \
   "-DarchetypeArtifactId=cucumber-archetype" \
   "-DarchetypeVersion=5.4.0"               \
   "-DgroupId=hellocucumber"                  \
   "-DartifactId=hellocucumber"               \
   "-Dpackage=hellocucumber"                  \
   "-Dversion=1.0.0-SNAPSHOT"                 \
   "-DinteractiveMode=false"

Or you can use IntelliJ to create a project with archetype;

Add archetype to your archetyhpes
Add interactiveMode property to your project properties

You should enable auto-import to give permission to maven for downloading dependencies.

Enable Auto-Import

Thus, our project was created. You can put our Gherkin files under src/test/resources/package-name in .feature format, you can find StepDefinitions class with step definitions and RunCucumberTest class under which we will make the necessary settings to run under src/test/java/package-name.

Now we can check from the terminal whether cucumber is installed correctly through the terminal in IntelliJ.

mvn test

should produce an output like below;

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.example.RunCucumberTest
Feb 29, 2020 6:25:15 PM io.cucumber.junit.Cucumber <init>

0 Scenarios
0 Steps
0m0.000s


Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.32 sec

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.393 s
[INFO] Finished at: 2020-02-29T18:25:15+03:00
[INFO] ------------------------------------------------------------------------

Let’s create our first feature file to resources/package-name folder with name plus.feature. I will make a calculator example for plus operation for the first BDD example. Let’s look at this example;

Feature: User wants to add two positive integer numbers
  - The sum of the two positive numbers must again be a positive number.

Scenario: The sum of two positive numbers should give a positive result
  Given We have the positive integers 3 and 5
  When I want to sum up these numbers
  Then I should get the answer 8

When you run mvn test again from your terminal, you should get;

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Scenario: The sum of two positive numbers should give a positive result # org/example/plus.feature:4
  Given We have the positive integers 3 and 5                           # null
  When I want to sum up these numbers                                   # null
  Then I should get the answer 8                                        # null

Undefined scenarios:
classpath:org/example/plus.feature:4# The sum of two positive numbers should give a positive result

1 Scenarios (1 undefined)
3 Steps (3 undefined)
0m0.131s


You can implement missing steps with the snippets below:

@Given("We have the positive integers {int} and {int}")
public void we_have_the_positive_integers_and(Integer int1, Integer int2) {
    // Write code here that turns the phrase above into concrete actions
    throw new io.cucumber.java.PendingException();
}

@When("I want to sum up these numbers")
public void i_want_to_sum_up_these_numbers() {
    // Write code here that turns the phrase above into concrete actions
    throw new io.cucumber.java.PendingException();
}

@Then("I should get the answer {int}")
public void i_should_get_the_answer(Integer int1) {
    // Write code here that turns the phrase above into concrete actions
    throw new io.cucumber.java.PendingException();
}


Tests run: 1, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.557 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 1

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.981 s
[INFO] Finished at: 2020-02-29T18:51:37+03:00
[INFO] ------------------------------------------------------------------------

And you can easily copy and paste Given When and Then code blocks to your StepDefinitions class.

Write your features with your mother language

If your company has determined your mother language as the language of correspondence; I guess that your analysis artifacts will also be in your native language. So you don’t want sentences mixed with English keywords in your analysis. When doing requirement refinement activities in Gherkin format, you use tags such as given, when and then. Cucumber has expanded these tags to support 74 different languages. Each tag has its equivalent in these 74 languages. If you want to look at the list, you can find it here. In order to write in your choice, you need to specify in which language you will write with # language: XX at the beginning of your feature file. You can find an example written in Turkish below.

# language: tr
Özellik: Kullanıcı iki pozitif tam sayıyı toplamak ister
  - İki pozitif tam sayının toplamının sonucu yine pozitif tam sayıdır.

Senaryo: İki pozitif tam sayının toplamının sonucu pozitif tam sayı olmalıdır.
  Diyelim ki elimizde iki tam sayı olarak 3 ve 5 var
  Eğer ki bu sayıları toplamak istersem
  O zaman sonuç olarak 8 almalıyım

You can find the source code from github; https://github.com/mgok/cucumberintenminutes.git

Feel free to ask any questions.

Thanks for reading.

Stay happy and healthy…

About Author

Leave a Comment

Your email address will not be published. Required fields are marked *