Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 37 ยท Projects and Practice

Project: Shopping Cart with Background and Outlines

A second BDD project: model a shopping cart with coupons and shipping rules, using Background, Scenario Outline tables and tags, run in a Java mini runner.

The bank account project had one kind of step and a small set of rules. This second project adds the features that make real feature files compact: a Background shared by every scenario, a Scenario Outline with an Examples table, and tags to select what runs. The domain is a shopping cart with a catalog, quantities, a coupon and a shipping rule.

Cucumber is not available on this site, so the example runs on a Java mini runner that understands Background, Scenario Outline, Examples and @tags. After running it you will map each part to the real Cucumber-JVM equivalent, and you will get a list of extensions to try.

The rules

Gather these with the business first (Three amigos):

  1. The subtotal is the sum of price times quantity for every line.
  2. The coupon SAVE10 takes 10 percent off the subtotal (rounded down to whole dollars to keep the examples simple). Unknown coupons are rejected.
  3. Shipping is 5 dollars, and free when the discounted subtotal is 50 dollars or more.
  4. The total is discounted subtotal plus shipping.
  5. Removing an item removes every unit of it.

The feature

GHERKIN
Feature: Shopping cart

  Background:
    Given the catalog has Pen at 3 dollars
    And the catalog has Notebook at 12 dollars
    And the catalog has Bag at 40 dollars
    And an empty cart

  @smoke
  Scenario: Adding items sums the subtotal
    When I add 2 Pen
    And I add 1 Notebook
    Then the subtotal is 18 dollars

  Scenario: Removing an item removes all its units
    Given I add 3 Pen
    When I remove Pen
    Then the cart has 0 items

  Scenario: An unknown coupon is rejected
    When I apply the coupon "FREE"
    Then the request is rejected with "Unknown coupon"

  @rules
  Scenario Outline: Totals including shipping and coupon
    When I add <qty> <item>
    And I apply the coupon "<coupon>"
    Then the total is <total> dollars

    Examples:
      | qty | item     | coupon | total |
      | 1   | Pen      | NONE   | 8     |
      | 4   | Notebook | NONE   | 53    |
      | 1   | Bag      | NONE   | 45    |
      | 2   | Bag      | SAVE10 | 72    |

Check the arithmetic: one pen is 3 plus 5 shipping = 8. Four notebooks are 48, under 50, so shipping adds 5 = 53. One bag is 40 plus 5 = 45. Two bags are 80, minus 10 percent = 72, free shipping, total 72.

The Background runs before every scenario and states facts all of them share. Keep it short (three or four lines) and relevant; a long Background pushes the real story out of sight, as warned in anti-patterns. The outline runs once per row, replacing <qty>, <item> and so on.

Run it

The runner expands each outline row into a scenario, prepends the Background steps, and filters by tag. Change TAG_FILTER to @smoke or @rules to run a subset, or set it to null to run everything.

Java Shopping cart with Background, Outline and tags (Java)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

The runner is deliberately small. You will build a cleaner version step by step in the mini Gherkin runner project.

Map it to real Cucumber

Mini runner Real Cucumber-JVM
step("I add (\\d+) (\\w+)", ...) @When("I add {int} {word}") on a method
catalog, cart static fields an injected world object, new per scenario (state)
TAG_FILTER -Dcucumber.filter.tags="@smoke" (tags)
resetting state before each scenario @Before hook
Background lines prepended Background steps run after @Before hooks, before the scenario's own steps
<qty> replacement Scenario Outline substitution, done by Cucumber before matching

The real glue for the cart is a plain class:

Java
public class CartSteps {
    private final CartWorld world;
    public CartSteps(CartWorld world) { this.world = world; }

    @Given("the catalog has {word} at {int} dollars")
    public void catalog(String item, int price) { world.catalog.put(item, price); }

    @Given("an empty cart")
    public void emptyCart() { world.cart = new Cart(world.catalog); }

    @When("I add {int} {word}")
    public void add(int qty, String item) { world.attempt(() -> world.cart.add(item, qty)); }

    @Then("the total is {int} dollars")
    public void total(int expected) { assertEquals(expected, world.cart.total()); }
}

Things to notice

  • One outcome type per outline. All four examples assert the total. The unknown coupon is a separate scenario because its Then differs.
  • Names in the Examples table matter. NONE as a coupon is a small trick to keep the table rectangular. In real life you might prefer a separate step When I apply no coupon and two outlines.
  • A rounding rule is hiding here. SAVE10 on 45 dollars would give 4.5; the code rounds the discount down with integer division. A real team would ask the business and add an example for it.

Extension ideas

  1. Add a rule: more than 10 pens gets a bulk discount. Write the example table first and watch rows fail.
  2. Add a @wip tag, and make the runner skip scenarios with it, using the evaluator from the tags lesson.
  3. Add a second coupon type, FREESHIP, and a scenario for combining it with SAVE10.
  4. Move the arithmetic checks into a unit test and keep only two cart scenarios in Gherkin, then compare speed and clarity (levels).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a Background do?

    Background steps run before each scenario of the feature, after any Before hooks.
  2. 2. How many scenarios does an outline with four Examples rows produce?

    Each row runs the outline once, with the placeholders replaced.
  3. 3. Why is the unknown-coupon case not a row in the totals outline?

    An outline should check one kind of outcome in all its rows.
  4. 4. Which Background is a smell?

    Long Backgrounds bury the point of each scenario and waste time creating unused data.

Summary

  • Background removes shared setup; Scenario Outline plus Examples removes repetition; tags select what runs.
  • Keep Backgrounds short and outlines focused on one outcome type.
  • The mini runner mirrors real Cucumber: parse, expand, match, run, report.
  • Rounding and boundary examples are where hidden rules appear; ask the business.

Next: Project: a mini Gherkin runner