articles / bdd-gherkin-cucumber
BDD with Gherkin and Cucumber: specifications that run
Mounir Guizani · 29 November 2026 · 4 min read
How many bugs come from wrong code? And how many from a misunderstood requirement? On Société Générale's refinancing platform, we wrote our acceptance tests in Gherkin. Here is what BDD brings, and how to use it without falling into its traps.
BDD is a conversation first
Behavior-Driven Development (BDD) starts from a simple observation: business people, developers and testers don't use the same words. BDD brings them together (the "Three Amigos": business, dev, test) to describe the expected behaviour with concrete examples, before writing the code.
These examples are written in a format everyone can read: Gherkin. Then a tool, Cucumber, runs them as automated tests.
Gherkin: specifications everyone reads
A simplified example, inspired by a receivables eligibility rule:
Feature: Eligibility of a receivable for mobilisation
Scenario: A long-term receivable is eligible
Given a receivable of 250000 euros
And a maturity in 400 days
When the system checks its eligibility
Then the receivable is eligible
Scenario: A receivable close to maturity is not eligible
Given a receivable of 250000 euros
And a maturity in 15 days
When the system checks its eligibility
Then the receivable is rejected with the reason "maturity too close"
Three keywords structure everything:
- Given: the starting context.
- When: the action.
- Then: the expected, observable outcome.
A business expert can read this file, correct it, and add the edge case nobody had seen. That is exactly the point.
Cucumber: linking sentences to code
Each sentence is linked to Java code by a step definition:
public class EligibilitySteps {
private final EligibilityService service = new EligibilityService();
private BigDecimal amount;
private LocalDate maturity;
private Decision decision;
@Given("a receivable of {int} euros")
public void a_receivable(int euros) {
amount = BigDecimal.valueOf(euros);
}
@Given("a maturity in {int} days")
public void a_maturity(int days) {
maturity = LocalDate.now().plusDays(days);
}
@When("the system checks its eligibility")
public void check() {
decision = service.check(new Receivable(amount, maturity));
}
@Then("the receivable is eligible")
public void eligible() {
assertTrue(decision.eligible());
}
@Then("the receivable is rejected with the reason {string}")
public void rejected(String reason) {
assertFalse(decision.eligible());
assertEquals(reason, decision.reason());
}
}
The result: the specification is the test. If the rule changes, the scenario changes, and the code has to follow. You get living documentation, always up to date, because it fails as soon as it lies.
BDD and TDD: two complementary levels
| TDD | BDD | |
|---|---|---|
| Question asked | "Is the code doing things right?" | "Are we doing the right thing?" |
| Level | class, function | feature, business journey |
| Readers | developers | business, testers, developers |
| Tools | JUnit, Mockito | Gherkin, Cucumber |
I combine them outside-in: a failing Gherkin scenario describes the need, then TDD cycles build the code until the scenario passes.
Traps I learned to avoid
- Overly technical scenarios. "When I click the #submit button" describes the UI, not the behaviour. Write what the user does, not how.
- Too many scenarios. BDD covers business rules and their edge cases, not every combination: unit tests handle that.
- Steps coupled to the UI. Prefer testing at the service or API level: faster, more stable.
- Writing alone. A Gherkin file written by a developer without the business is just a more verbose test. The value is in the conversation.
What it changes on a project
On a high-volume SAFe project, the Gherkin scenarios acted as a contract between the business and the team. Ambiguities surfaced in workshops, before development, not in acceptance testing. Every mobilisation rule had its executable examples: when in doubt about a behaviour, we read the scenario rather than the code.
BDD doesn't replace other tests. It ensures we build the right thing, and that everyone shares the same definition of it.