1. Introduction

Here we will understand a little about BDD by executing some projects (or creating them) through tutorials written with different languages and frameworks:

2. Java sample with Cucumber

I created this example in 2013. So, this code is very old (like Java). Read my original blog post about it (written in Brazilian Portuguese).

2.1. Java version

$ java -version
openjdk version "1.8.0_275"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_275-b01)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.275-b01, mixed mode)

2.2. Final code (created by following the tutorial below)

$ git clone https://github.com/paulojeronimo/java-cucumber-sample

2.3. Final code test

$ cd java-cucumber-sample
$ git checkout en; mvn test
$ git checkout pt-br; mvn clean test

2.4. Tutorial

$ d=java-cucumber-sample; rm -rf $d && mkdir -p $d && cd $d
$ d=src/test/resources/com/ladoservidor/cucumber/helloworld; mkdir -p $d
$ cat > $d/helloworld.feature <<'EOF'
Feature: Hello World

  Scenario: Say hello
    Given I have a hello app with "Hello"
    When I ask it to say hi
    Then it should answer with "Hello World"
EOF
$ cat > pom.xml <<'EOF'
<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">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ladoservidor</groupId>
    <artifactId>cucumber-jvm-helloworld</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <name>cucumber-jvm/HelloWorld</name>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.2</version>
                <configuration>
                    <useFile>false</useFile>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>1.1.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>1.1.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
EOF
$ d=src/test/java/com/ladoservidor/cucumber/helloworld; mkdir -p $d
$ cat > $d/RunCukesTest.java <<'EOF'
package com.ladoservidor.cucumber.helloworld;

import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@Cucumber.Options(
  format = {
    "pretty",
    "html:target/cucumber-html-report",
    "json-pretty:target/cucumber-json-report.json"
  }
)
public class RunCukesTest {
}
EOF
$ mvn test
$ tree target
$ open target/cucumber-html-report/index.html
$ cat > $d/HelloStepdefs.java <<'EOF'
package com.ladoservidor.cucumber.helloworld;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

import static org.junit.Assert.assertEquals;

public class HelloStepdefs {
    private Hello hello;
    private String hi;

    @Given("^I have a hello app with \"([^\"]*)\"$")
    public void I_have_a_hello_app_with(String greeting) {
        hello = new Hello(greeting);
    }

    @When("^I ask it to say hi$")
    public void I_ask_it_to_say_hi() {
        hi = hello.sayHi();
    }

    @Then("^it should answer with \"([^\"]*)\"$")
    public void it_should_answer_with(String expectedHi) {
        assertEquals(expectedHi, hi);
    }
}
EOF
$ d=src/main/java/com/ladoservidor/cucumber/helloworld; mkdir -p $d
$ cat > $d/Hello.java <<'EOF'
package com.ladoservidor.cucumber.helloworld;

public class Hello {
    private final String greeting;

    public Hello(String greeting) {
        this.greeting = greeting;
    }

    public String sayHi() {
        return greeting + " World";
    }
}
EOF
$ echo target > .gitignore
$ cat > README.adoc <<'EOF'
= java-cucumber-sample

Read https://paulojeronimo.com/bdd-tutorial/#java-cucumber-sample.

EOF
$ git init; git add .; git commit -m 'Initial commit'
$ git tag en
$ d=src/test/resources/com/ladoservidor/cucumber/helloworld
$ cat > $d/helloworld.feature <<'EOF'
# language: pt
Funcionalidade: Diga Olá

  Cenário: Dizer "Olá Fulano!"
    Dado que eu tenho uma app que recebe "Paulo"
    Quando eu pedir que ela diga olá
    Então ela deveria responder "Olá Paulo!"
EOF
$ patch -p1 <<'EOF'
--- ./src/test/java/com/ladoservidor/cucumber/helloworld/RunCukesTest.java  2013-04-05 15:44:14.000000000 -0300
+++ ../HelloWorld.pt/src/test/java/com/ladoservidor/cucumber/helloworld/RunCukesTest.java 2013-04-05 15:45:15.000000000 -0300
@@ -8,7 +8,8 @@ import org.junit.runner.RunWith;
   format = {
     "pretty",
     "html:target/cucumber-html-report",
-    "json-pretty:target/cucumber-json-report.json"
+    "json-pretty:target/cucumber-json-report.json",
+    "json:target/cucumber-pt.json"
   }
 )
 public class RunCukesTest {
EOF
$ patch -p1 <<'EOF'
--- ./src/test/java/com/ladoservidor/cucumber/helloworld/HelloStepdefs.java 2013-04-05 15:44:14.000000000 -0300
+++ ../HelloWorld.pt/src/test/java/com/ladoservidor/cucumber/helloworld/HelloStepdefs.java  2013-04-05 15:45:15.000000000 -0300
@@ -1,8 +1,8 @@
 package com.ladoservidor.cucumber.helloworld;

-import cucumber.api.java.en.Given;
-import cucumber.api.java.en.Then;
-import cucumber.api.java.en.When;
+import cucumber.api.java.pt.Dado;
+import cucumber.api.java.pt.Quando;
+import cucumber.api.java.pt.Entao;

 import static org.junit.Assert.assertEquals;

@@ -10,17 +10,17 @@ public class HelloStepdefs {
     private Hello hello;
     private String hi;

-    @Given("^I have a hello app with \"([^\"]*)\"$")
+    @Dado("^que eu tenho uma app que recebe \"([^\"]*)\"$")
     public void I_have_a_hello_app_with(String greeting) {
         hello = new Hello(greeting);
     }

-    @When("^I ask it to say hi$")
+    @Quando("^eu pedir que ela diga olá$")
     public void I_ask_it_to_say_hi() {
         hi = hello.sayHi();
     }

-    @Then("^it should answer with \"([^\"]*)\"$")
+    @Entao("^ela deveria responder \"([^\"]*)!\"$")
     public void it_should_answer_with(String expectedHi) {
         assertEquals(expectedHi, hi);
     }
EOF
$ mvn clean test
$ patch -p1 <<'EOF'
--- ./src/main/java/com/ladoservidor/cucumber/helloworld/Hello.java 2013-04-05 15:44:14.000000000 -0300
+++ ../HelloWorld.pt/src/main/java/com/ladoservidor/cucumber/helloworld/Hello.java  2013-04-05 15:45:15.000000000 -0300
@@ -8,6 +8,6 @@ public class Hello {
     }

     public String sayHi() {
-        return greeting + " World";
+        return "Olá " + greeting;
     }
 }
EOF
$ mvn clean test
$ git add . && git commit -m 'Added support to pt-br'
$ git tag pt-br

If you are "Paulo Jerônimo" (hehehe) push this code this way:

$ git remote add origin git@github.com:paulojeronimo/java-cucumber-sample.git
$ git branch -M main
$ git push -u origin main -f --tags

2.5. Explore more

3. JavaScript sample with Cucumber

3.1. Node and Yarn versions

$ node -v
v12.18.3
$ yarn -v
1.22.4

3.2. Initial and final code download

$ git clone https://github.com/paulojeronimo/javascript-cucumber-sample
$ cd javascript-cucumber-sample; javascript_cucumber_sample=$PWD

3.3. Final code test

$ cd final
$ yarn install
$ yarn test

3.4. Tutorial

$ d=$javascript_cucumber_sample/tutorial; rm -rf $d && mkdir $d && cd $d
$ yarn init -y
$ yarn add @cucumber/cucumber --dev
$ echo node_modules > .gitignore
$ git init; git add .
$ git apply < ../patches/package.json.diff
$ (cd ../initial; rsync -R features/is_it_friday_yet.feature ../tutorial/)
$ yarn test
$ mkdir -p features/step_definitions
$ cat > features/step_definitions/steps.js <<EOF
const { Given, When, Then } = require('@cucumber/cucumber')

$(`yarn bin`/cucumber-js --format snippets 2> /dev/null)
EOF
$ yarn test
$ git add .
$ cp ../initial/features/step_definitions/steps.js features/step_definitions/
$ yarn test
$ git add .
$ git apply ../patches/features/step_definitions/steps.js.2.diff
$ yarn test
$ git add .
$ git apply ../patches/features/is_it_friday_yet.feature.diff
$ git apply ../patches/features/step_definitions/steps.js.3.diff
$ yarn test
$ git add .
$ git apply ../patches/features/step_definitions/steps.js.4.diff
$ yarn test
$ git apply ../patches/features/is_it_friday_yet.feature.2.diff
$ git apply ../patches/features/step_definitions/steps.js.5.diff
$ yarn test
$ git add .
$ git commit -m 'Initial commit'

If you are "Paulo Jerônimo" (hehehe) push this code this way:

$ cd ..
$ rsync -av --exclude .git --exclude node_modules tutorial/ final/
$ git remote add origin git@github.com:paulojeronimo/javascript-cucumber-sample.git
$ git branch -M main
$ git push -u origin main -f --tags

4. TypeScript sample with Jest Cucumber

4.1. Node and Yarn versions

$ node -v
v12.18.3
$ yarn -v
1.22.4

4.2. Initial and final code download

$ git clone https://github.com/paulojeronimo/typescript-jest-cucumber-sample
$ cd typescript-jest-cucumber-sample; typescript_jest_cucumber_sample=$PWD

4.2.1. Final code test

$ cd final
$ yarn install
$ yarn test

4.3. Tutorial

d=$typescript_jest_cucumber_sample/tutorial; rm -rf $d && mkdir $d && cd $d
$ yarn init -y
$ yarn add typescript
$ yarn add jest @types/jest ts-node ts-jest --dev
$ `yarn bin`/tsc --init
$ `yarn bin`/jest --init
Would you like to use Typescript for the configuration file? › (y/N) yes
$ echo node_modules > .gitignore
$ git init; git add .
$ yarn add jest-cucumber --dev
$ git apply ../patches/jest.config.ts.diff
$ (cd ../initial; rsync -R cucumber/features/basic-scenarios.feature ../tutorial/)
$ (cd ../initial; rsync -R cucumber/step-definitions/basic-scenarios.steps.ts ../tutorial/)
$ yarn test
$ git apply ../patches/cucumber/features/basic-scenarios.steps.ts.diff
$ (cd ../initial; rsync -R src/password-validator.ts ../tutorial/)
$ yarn test
$ git add .
$ git commit -m 'Initial commit'

If you are "Paulo Jerônimo" (hehehe) push this code this way:

$ cd ..
$ rsync -av --exclude .git --exclude node_modules tutorial/ final/
$ git remote add origin git@github.com:paulojeronimo/typescript-jest-cucumber-sample.git
$ git branch -M main
$ git push -u origin main -f --tags

5. Groovy sample with Spock Framework

6. Kotlin sample with Spek Framework

Comming soon …​