Skip to content
Back to search

tzatziki

Decathlon library to ease and promote Test Driven Development of Java microservices!

Safety scan

A static text scan, not a guarantee. Select the badge to see what was scanned and every finding.

Checked today
Badge for your README
findsafeskills safety rating

Markdown

[![findsafeskills safety rating](/api/listings/decathlon--tzatziki/badge.svg)](/listings/decathlon--tzatziki)

HTML

<a href="/listings/decathlon--tzatziki"><img src="/api/listings/decathlon--tzatziki/badge.svg" alt="findsafeskills safety rating" /></a>

Install

i

Install instructions from the README

As written by the repo's author. findsafeskills has not verified these steps.

The Cucumber tests are written in `.feature` files. Most of the IDEs have support for writting, running and debugging cucumber tests. Since deep down they are just JUnit tests, once they are running everything should be the same: code coverage, reporting etc.

The structure of a `.feature` file is the following:

Feature: the name of your feature
  Here you can put some comments describing your feature

  Background: some stuff that needs to be done for every scenario
    * a system is running

  @this-is-a-tag
  Scenario: Change a state in the system
  As a User I expect to go from A to C if B happens

    Given a state A
    When B happens
    # we can also put comments if things need a bit of explanation
    Then C is the new state of the system

  Scenario: some other scenario
  ...

The lines starting with `Given, When, Then` are called Steps. Additional Steps keywords are `And` and `But` (`*` is also accepted). Those keywords don't really have a functional meaning, they are just there for us to write nice tests. We could start every step with `*` and the output of the test would be exactly the same. However, you should choose the one fitting the most the intent of the step you are writing. A big part of the idea behind using Gherkin, is that the tests are the specifications of the code, so it should be enough to read them to understand the product they test.

An optional `Background` section can be added at the beginning. The steps in it will be repeated before any scenario in the file, like a method annotated with `@org.junit.jupiter.api.BeforeEach`.

Each Step has an implementation in plain Java that is annotated with a regular expression matching the step.

So for example:

Given that we do something

will have the following implementation:

@Given("that we do something")
public void do_something(){
  // do something here
}

Cucumber can extract parameters directly from a step so that:

Given a user named "bob"

can be implemented as:

@Given("a user named \"(.*)\"")
public void a_user_named(String name){
  // create a user with that name
}

But it also supports multiline arguments:

Given the following configuration file:
  """
  property: value
  """
@Given("the following configuration file:")
public void the_following_configuration_file(String content){
  // do something with the file content
}

as well as tables:

Given the following users:
  | id  | name    |
  | 1   | bob     |
  | 2   | alice   |
@Given("the following users")
public void the_following_users(List<Map<String, String>> users){
  // do something with those users
}

Those Java methods need to be added to a Steps class, typically something like `LocalSteps`. Keep in mind that for technical reasons Cucumber will not allow you to extend those steps. Instead, the framework will enforce composition, and if any class extending a Steps class is detected, an exception will be thrown.

Cucumber also comes with support for injection frameworks, so all your dependencies will be properly instantiated and injected at runtime, per scenario.

Note that your `@org.junit.jupiter.api.BeforeEach` and `@org.junit.jupiter.api.AfterEach` annotations won't work in your steps. You need to use the Cucumber equivalent: `@cucumber.api.java.Before` and `@cucumber.api.java.After`

Example:

public class BaseSteps {

    @Before
    public void before() {
        // something to run before each scenario
    }

    @Given("that we do something")
    public void do_something() {
        // do something here
    }
}

public class LocalSteps {

    private final BaseSteps baseSteps;

    public LocalSteps(BaseSteps baseSteps) {
        this.baseSteps = baseSteps;
    }

    @Given("a user named \"(.*)\"")
    public void a_user_named(String name) {
        baseSteps.do_something();
        // create a user with that name
    }

What was scanned

The files the safety scan read, as written by the repository's author and shown as plain text. Lines with a finding are marked.

README.md · README · 282 lines

View this file on GitHub (read at the repository's current version: the scan did not record which commit it read)

  1. Tzatziki Steps Library
  2. ======
  3. [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.decathlon.tzatziki/tzatziki-parent/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.decathlon.tzatziki/tzatziki-parent)
  4. ![Build](https://github.com/Decathlon/tzatziki/workflows/Build/badge.svg)
  5. [![codecov](https://codecov.io/gh/Decathlon/tzatziki/branch/main/graph/badge.svg)](https://codecov.io/gh/Decathlon/tzatziki)
  6. [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
  7. ![lifecycle: beta](https://img.shields.io/badge/lifecycle-beta-509bf5.svg)
  8. This project is a collection of ready-to-use Cucumber steps making it easy to TDD Java microservices by focusing on an
  9. outside-in testing strategy.
  10. ## Wait, Cucumber?
  11. *You are a Cucumber veteran? ... jump directly to [Content of this project](#content-of-this-project)*
  12. Otherwise, here is what [wikipedia](https://en.wikipedia.org/wiki/Cucumber_(software)) says:
  13. > Cucumber is a software tool used by computer programmers for testing other software.
  14. > It runs automated acceptance tests written in a behavior-driven development (BDD) style.
  15. > Central to the Cucumber BDD approach is its plain language parser called Gherkin.
  16. > It allows expected software behaviors to be specified in a logical language that customers can understand.
  17. > As such, Cucumber allows the execution of feature documentation written in business-facing text.
  18. *What does it mean to us developers?*
  19. Cucumber provides a mapping between humanly readable test files written in a language called Gherkin and their JUnit
  20. implementations. You can think about it as a partition that will execute pieces of JUnit code.
  21. *Why using Cucumber?*
  22. By creating a separation between a test expression and its implementation, the resulting Cucumber test tends to be a bit
  23. more readable than its JUnit counterpart. Additionaly, the reusability of each JUnit implementation is really high, and
  24. over time only the Gherkin needs to be added to test a new feature.
  25. *Okay ... so how does it work?*
  26. ### Getting started with Cucumber in 5 mins
  27. The Cucumber tests are written in `.feature` files. Most of the IDEs have support for writting, running and debugging
  28. cucumber tests. Since deep down they are just JUnit tests, once they are running everything should be the same: code
  29. coverage, reporting etc.
  30. The structure of a `.feature` file is the following:
  31. ```gherkin
  32. Feature: the name of your feature
  33. Here you can put some comments describing your feature
  34. Background: some stuff that needs to be done for every scenario
  35. * a system is running
  36. @this-is-a-tag
  37. Scenario: Change a state in the system
  38. As a User I expect to go from A to C if B happens
  39. Given a state A
  40. When B happens
  41. # we can also put comments if things need a bit of explanation
  42. Then C is the new state of the system
  43. Scenario: some other scenario
  44. ...
  45. ```
  46. The lines starting with `Given, When, Then` are called Steps. Additional Steps keywords are `And` and `But` (`*` is also
  47. accepted). Those keywords don't really have a functional meaning, they are just there for us to write nice tests. We
  48. could start every step with `*` and the output of the test would be exactly the same. However, you should choose the one
  49. fitting the most the intent of the step you are writing. A big part of the idea behind using Gherkin, is that the tests
  50. are the specifications of the code, so it should be enough to read them to understand the product they test.
  51. An optional `Background` section can be added at the beginning. The steps in it will be repeated before any scenario in
  52. the file, like a method annotated with `@org.junit.jupiter.api.BeforeEach`.
  53. Each Step has an implementation in plain Java that is annotated with a regular expression matching the step.
  54. So for example:
  55. ```gherkin
  56. Given that we do something
  57. ```
  58. will have the following implementation:
  59. ```java
  60. @Given("that we do something")
  61. public void do_something(){
  62. // do something here
  63. }
  64. ```
  65. Cucumber can extract parameters directly from a step so that:
  66. ```gherkin
  67. Given a user named "bob"
  68. ```
  69. can be implemented as:
  70. ```java
  71. @Given("a user named \"(.*)\"")
  72. public void a_user_named(String name){
  73. // create a user with that name
  74. }
  75. ```
  76. But it also supports multiline arguments:
  77. ```gherkin
  78. Given the following configuration file:
  79. """
  80. property: value
  81. """
  82. ```
  83. ```java
  84. @Given("the following configuration file:")
  85. public void the_following_configuration_file(String content){
  86. // do something with the file content
  87. }
  88. ```
  89. as well as tables:
  90. ```gherkin
  91. Given the following users:
  92. | id | name |
  93. | 1 | bob |
  94. | 2 | alice |
  95. ```
  96. ```java
  97. @Given("the following users")
  98. public void the_following_users(List<Map<String, String>> users){
  99. // do something with those users
  100. }
  101. ```
  102. Those Java methods need to be added to a Steps class, typically something like `LocalSteps`. Keep in mind that for
  103. technical reasons Cucumber will not allow you to extend those steps. Instead, the framework will enforce composition,
  104. and if any class extending a Steps class is detected, an exception will be thrown.
  105. Cucumber also comes with support for injection frameworks, so all your dependencies will be properly instantiated and
  106. injected at runtime, per scenario.
  107. Note that your `@org.junit.jupiter.api.BeforeEach` and `@org.junit.jupiter.api.AfterEach` annotations won't work in your steps. You need to use the
  108. Cucumber equivalent: `@cucumber.api.java.Before` and `@cucumber.api.java.After`
  109. Example:
  110. ```java
  111. public class BaseSteps {
  112. @Before
  113. public void before() {
  114. // something to run before each scenario
  115. }
  116. @Given("that we do something")
  117. public void do_something() {
  118. // do something here
  119. }
  120. }
  121. public class LocalSteps {
  122. private final BaseSteps baseSteps;
  123. public LocalSteps(BaseSteps baseSteps) {
  124. this.baseSteps = baseSteps;
  125. }
  126. @Given("a user named \"(.*)\"")
  127. public void a_user_named(String name) {
  128. baseSteps.do_something();
  129. // create a user with that name
  130. }
  131. @Given("the following users")
  132. public void the_following_users(List<Map<String, String>> users) {
  133. // do something with those users
  134. }
  135. @After
  136. public void after() {
  137. // something to run after each scenario
  138. }
  139. }
  140. ```
  141. Finally, in order to have JUnit execute our Cucumber tests we need a runner:
  142. ```java
  143. package com.yourcompany.yourproject;
  144. import io.cucumber.junit.platform.engine.Constants;
  145. import org.junit.platform.suite.api.ConfigurationParameter;
  146. import org.junit.platform.suite.api.IncludeEngines;
  147. import org.junit.platform.suite.api.SelectClasspathResource;
  148. import org.junit.platform.suite.api.Suite;
  149. @Suite
  150. @IncludeEngines("cucumber")
  151. @SelectClasspathResource("com/yourcompany/yourproject")
  152. @ConfigurationParameter(key = Constants.PLUGIN_PROPERTY_NAME, value = "pretty")
  153. public class CucumberTest {
  154. }
  155. ```
  156. By default, cucumber will look for `.feature` files in the same directory structure than the java runner. However, this
  157. can be configured using the `features` property on the `@io.cucumber.junit.CucumberOptions` annotation. In addition, it
  158. will also look for Java classes containing steps next to the runner and this can also be configured by using the `glues`
  159. property on the same annotation, as illustrated below:
  160. ```java
  161. @Suite
  162. @IncludeEngines("cucumber")
  163. @SelectClasspathResource("features")
  164. @ConfigurationParameter(key = Constants.PLUGIN_PROPERTY_NAME, value = "pretty")
  165. @ConfigurationParameter(key = Constants.GLUE_PROPERTY_NAME, value = "com.yourcompany.yourproject.features,com.decathlon.tzatziki.steps")
  166. @ConfigurationParameter(key = Constants.FILTER_TAGS_PROPERTY_NAME, value = "not @ignore")
  167. public class CucumberTest {
  168. }
  169. ```
  170. > Tip:
  171. > Sometimes it can be hard to come up with the implementation steps...
  172. > but if you start by typing your new step in your feature file and then execute the scenario, Cucumber will output an implementation for you:
  173. ```
  174. Undefined step: Given something else that is not yet implemented
  175. Skipped step
  176. Skipped step
  177. Skipped step
  178. 1 Scenarios (1 undefined)
  179. 5 Steps (3 skipped, 1 undefined, 1 passed)
  180. 0m0.250s
  181. You can implement missing steps with the snippets below:
  182. @Given("^something else that is not yet implemented")
  183. public void something_else_that_is_not_yet_implemented() throws Throwable {
  184. // Write code here that turns the phrase above into concrete actions
  185. throw new PendingException();
  186. }
  187. ```
  188. ## Content of this project
  189. This repository contains several libraries, each one having its own tutorial and documentation when applicable:
  190. - [tzatziki-mapper](https://github.com/Decathlon/tzatziki/tree/main/tzatziki-mapper) : module containing only the Mapper
  191. interface.
  192. - [tzatziki-jackson](https://github.com/Decathlon/tzatziki/tree/main/tzatziki-jackson) : Jackson implementation of the
  193. Mapper.
  194. - [tzatziki-jackson3](https://github.com/Decathlon/tzatziki/tree/main/tzatziki-jackson3) : Jackson 3 (`tools.jackson`)
  195. implementation of the Mapper. Jackson 3 is selected whenever `tzatziki-jackson3` is on the classpath; otherwise Jackson 2
  196. is selected.
  197. - [tzatziki-common](https://github.com/Decathlon/tzatziki/tree/main/tzatziki-common) : dependency module containing the
  198. base classes for the core library, but without cucumber.
  199. - [tzatziki-core](https://github.com/Decathlon/tzatziki/tree/main/tzatziki-core) : the core library, provides support of
  200. our test instances as well as input/output and time management.
  201. - [tzatziki-logback](https://github.com/Decathlon/tzatziki/tree/main/tzatziki-logback) : the logging library, provides
  202. support for dynamically configuring the log levels in your tests.
  203. - [tzatziki-http](https://github.com/Decathlon/tzatziki/tree/main/tzatziki-http) : http library encapsulating both
  204. rest-assured and WireMock.
  205. - [tzatziki-spring](https://github.com/Decathlon/tzatziki/tree/main/tzatziki-spring) : base library to start a spring
  206. service
  207. - [tzatziki-spring-jpa](https://github.com/Decathlon/tzatziki/tree/main/tzatziki-spring-jpa) : support for spring jpa to
  208. insert and assert data in the database.
  209. - [tzatziki-spring-kafka](https://github.com/Decathlon/tzatziki/tree/main/tzatziki-spring-kafka) : support for spring
  210. kafka listener and consumers.
  211. ## Support
  212. We welcome [contributions](https://github.com/Decathlon/tzatziki/tree/main/CONTRIBUTING.md), opinions, bug reports and
  213. feature requests!

Ratings and comments

Sign in to rate this skill.

Sign in

Free to use. If it’s helped, pass it on.