DocStrings
Gherkin doc strings (triple-quoted blocks) become a trailing String parameter on the step method, with formatting preserved via Java text blocks.
Source: examples/common-use-cases/example-2 on GitHub.
What this demonstrates
- Doc strings become a trailing
Stringparameter on the step method - Method naming is unaffected by the doc string (no extra
$pplaceholder) - Formatting (newlines, indentation) is preserved via Java text blocks
- Doc strings can be combined with quoted parameters in the same step
- Content can be anything: JSON, plain text, XML, …
- A doc string may carry a content type (
json,image,svg,html) — the IDE highlights or previews it, while SpecBinder still passes a plainString
Doc string only
When I submit the following shipping address:
"""
{
"line1": "Baker St 221B",
"city": "London"
}
"""
// Step method — one String parameter for the doc string
public abstract void iSubmitTheFollowingShippingAddress(String docString);
// Generated call site — Java text block preserves formatting
iSubmitTheFollowingShippingAddress("""
{
"line1": "Baker St 221B",
"city": "London"
}
""");
Quoted args + doc string
When I add item "Wireless Headphones" with options:
"""
{ "color": "Black" }
"""
// Quoted args come first, doc string is appended after
public abstract void iAddItem$p1WithOptions(String p1, String docString);
iAddItem$p1WithOptions("Wireless Headphones", """
{ "color": "Black" }
""");
Content types
A doc string may name its content type right after the opening fence. The last scenario of the example uses three of them — the shop's branding assets as inputs, and the confirmation page it produces as the expected outcome:
Given the shop uses the following confirmation badge:
"""image
iVBORw0KGgoAAAANSUhEUgAAAIwAAABpCAIAAABnK1xY...
"""
And the shop uses the following delivery icon:
"""svg
<svg xmlns="http://www.w3.org/2000/svg" width="120" height="72" viewBox="0 0 120 72">...</svg>
"""
And I have completed a purchase
Then I should receive the following confirmation page:
"""html
<h2>Thank you for your order!</h2>
...
"""
image holds a picture base64 encoded — PNG, JPEG, GIF and WebP all work. svg holds the markup of a drawing and
html a fragment of a page. The generated signatures are the same as for any other doc string:
public abstract void theShopUsesTheFollowingConfirmationBadge(String docString);
public abstract void theShopUsesTheFollowingDeliveryIcon(String docString);
public abstract void iShouldReceiveTheFollowingConfirmationPage(String docString);
So nothing about the content type reaches the test code. It is there for the reader, and for the editor: the SpecBinder IntelliJ plugin renders the picture, the drawing or the page below the doc string — see Inline previews.
Implementing the steps
The concrete ShoppingCartTest receives each doc string exactly as written, so text can be compared verbatim — line
breaks included. The confirmation text and the HTML page are both built from the same completed order:
@Override
public void iShouldReceiveTheFollowingConfirmationPage(String expectedPage) {
assertEquals(expectedPage, completedOrder.confirmationPage());
}
ShoppingCartFeature.java (marker class, bare @Gherkin2JUnit)
└→ ShoppingCartScenarios.java (generated, abstract)
└→ ShoppingCartTest.java (your concrete class, implements the steps)
Run it
cd examples/common-use-cases/example-2
mvn test