Localization
Write Gherkin specs in the language your team uses to discuss the feature. Set the keyword language once on a marker class so the specs it discovers can use that language without a # language: header in every file.
Source: examples/going-further/example-10 on GitHub.
What this demonstrates
keywordLanguagescan be shared across marker classes through a base class- A
# language:header takes precedence for an individual file - Generated JUnit display names keep the spec's keywords, and step method names come from the step text by default
keywordSynonymsadds or replaces keyword spellings for the specs a marker discovers
Set the language on the marker class
Set keywordLanguages to German on Kasse.java. A bare @Gherkin2JUnit discovers specs next to the marker class by convention.
package dev.specbinder.examples.goingfurther.localization;
import dev.specbinder.annotations.Gherkin2JUnit;
import dev.specbinder.annotations.Gherkin2JUnitOptions;
import dev.specbinder.annotations.KeywordLanguage;
@Gherkin2JUnit
@Gherkin2JUnitOptions(keywordLanguages = {KeywordLanguage.GERMAN})
public abstract class Kasse {
}
Place Kasse.specb next to the marker. The file starts with Funktionalität: and uses German keywords throughout.
Funktionalität: Kasse
Regel: Kartenzahlung
Szenario: Mit Karte bezahlen
Gegeben sei ein Warenkorb mit "2" Artikeln
Wenn ich mit Karte bezahle
Dann hat die Bestellung den Status "bezahlt"
Und der Warenkorb ist leer
To share the language setting across markers, put the option on a base class they extend. A marker that sets its own list replaces the inherited list. See Config Inheritance.
If a marker covers specs written in different languages, list those languages in keywordLanguages. A file without a header can also mix keywords from the list. See the keywordLanguages reference.
Generated output
The German Regel becomes a JUnit @Nested class. SpecBinder keeps the German keywords in the display names and removes the full Gegeben sei keyword when deriving the first step method.
@DisplayName("Funktionalität: Kasse")
public abstract class KasseScenarios extends Kasse {
public abstract void einWarenkorbMit$p1Artikeln(Integer p1);
public abstract void ichMitKarteBezahle();
public abstract void hatDieBestellungDenStatus$p1(String p1);
public abstract void derWarenkorbIstLeer();
@Nested
@DisplayName("Regel: Kartenzahlung")
public class Rule_1 {
@Test
@DisplayName("Szenario: Mit Karte bezahlen")
public void rule_1_scenario_1() {
einWarenkorbMit$p1Artikeln(2);
ichMitKarteBezahle();
hatDieBestellungDenStatus$p1("bezahlt");
derWarenkorbIstLeer();
}
}
}
The language applies to keywords. Quoted values follow the usual type inference rules. Here "2" becomes an Integer and "bezahlt" becomes a String. Use "3.5" for a decimal, since "3,5" is read as text, and "true" or "false" for booleans. See Step Parameters.
Set the language for one file
Caisse.specb sits next to Kasse.specb and shares its marker. Its # language: fr header takes precedence over keywordLanguages for that file.
# language: fr
Fonctionnalité: Caisse
Règle: Paiement par carte
Scénario: Payer par carte
Soit un panier avec "2" articles
Quand je paie par carte
Alors la commande a le statut "payée"
Et le panier est vide
SpecBinder uses English when neither the header nor keywordLanguages selects a language.
Add your team's own keywords
KasseMitSynonymen extends Kasse to inherit German. It lives in a separate package with its own spec and declares one @KeywordSynonym entry per keyword it customizes.
@Gherkin2JUnit
@Gherkin2JUnitOptions(keywordSynonyms = {
@KeywordSynonym(keyword = GherkinKeyword.RULE, synonyms = "Geschäftsregel", replace = true),
@KeywordSynonym(keyword = GherkinKeyword.GIVEN, synonyms = "Vorausgesetzt")
})
public abstract class KasseMitSynonymen extends Kasse {
}
- With
replace = true,Geschäftsregelbecomes the only rule keyword for this marker. - By default, a synonym adds to the selected language's keywords.
Vorausgesetztis another way to write a Given step alongsideGegeben seiandAngenommen.
Use the colon after Geschäftsregel in the spec. The annotation takes the spelling "Geschäftsregel" on its own.
Funktionalität: Kasse
Geschäftsregel: Kartenzahlung
Szenario: Mit Karte bezahlen
Vorausgesetzt ein Warenkorb mit "2" Artikeln
Wenn ich mit Karte bezahle
Dann hat die Bestellung den Status "bezahlt"
Und der Warenkorb ist leer
JUnit displays the rule as Geschäftsregel: Kartenzahlung. With the default naming options, the first step generates einWarenkorbMit$p1Artikeln(Integer p1) from the text after Vorausgesetzt.
Synonyms apply after SpecBinder selects the language, including for specs with a # language: header. You can put shared synonyms on a base class. An entry on the marker replaces the inherited entry for that keyword only. See the keywordSynonyms reference for the syntax and validation rules.
Use Narrative: as a feature heading
CheckoutNarrative adds Narrative alongside the English feature keywords Feature, Ability, and Business Need.
@Gherkin2JUnit
@Gherkin2JUnitOptions(keywordSynonyms = {
@KeywordSynonym(keyword = GherkinKeyword.FEATURE, synonyms = "Narrative")
})
public abstract class CheckoutNarrative {
}
The heading and the As a / I want to / So that format are inspired by JBehave's story grammar.
Narrative: Pay by card at checkout
As a shopper
I want to pay for the items in my cart by card
So that I can complete my purchase without cash
Rule: A successful card payment completes the order and empties the cart
Scenario: Pay by card for two items
Given a cart with "2" items
When I pay by card
Then the order status is "paid"
And the cart is empty
The three story lines form the feature description. The generated class uses Narrative: Pay by card at checkout as its JUnit display name, and the scenario under the rule becomes a runnable test.
Run it
Run the example from the SpecBinder repository root with JDK 21.
./mvnw -pl examples/going-further/example-10 -am test
All four checkout scenarios should pass. Open target/generated-test-sources/test-annotations/ in the example module to see the generated Java and its display names.
Tooling support
- The IntelliJ plugin reads a spec with the same keywords as the generator: its
# language:header and the marker'skeywordLanguagesandkeywordSynonyms. Highlighting, keyword completion, the Structure and Project views, running, navigation, debugging and Find Usages all work as they do for an English spec, and edits to these settings take effect in open specs straight away. - The execution reporter uses the same keyword languages and synonyms as the generator and preserves the original step text in reports.
All languages
Use the Gherkin code in a # language: header and the KeywordLanguage constant in keywordLanguages.
Supported languages and codes
| Constant | Gherkin code | Language |
|---|---|---|
AFRIKAANS | af | Afrikaans |
ARMENIAN | am | Armenian (հայերեն) |
AMHARIC | amh | Amharic (አማርኛ) |
ARAGONESE | an | Aragonese (Aragonés) |
ARABIC | ar | Arabic (العربية) |
ASTURIAN | ast | Asturian (asturianu) |
AZERBAIJANI | az | Azerbaijani (Azərbaycanca) |
BELARUSIAN | be | Belarusian (Беларуская) |
BULGARIAN | bg | Bulgarian (български) |
MALAY | bm | Malay (Bahasa Melayu) |
BOSNIAN | bs | Bosnian (Bosanski) |
CATALAN | ca | Catalan (català) |
CZECH | cs | Czech (Česky) |
WELSH | cy-GB | Welsh (Cymraeg) |
DANISH | da | Danish (dansk) |
GERMAN | de | German (Deutsch) |
GREEK | el | Greek (Ελληνικά) |
EMOJI | em | Emoji (😀) |
ENGLISH | en | English |
ENGLISH_SCOUSE | en-Scouse | Scouse |
ENGLISH_AUSTRALIAN | en-au | Australian |
ENGLISH_LOLCAT | en-lol | LOLCAT |
ENGLISH_OLD | en-old | Old English (Englisc) |
ENGLISH_PIRATE | en-pirate | Pirate |
ENGLISH_TEXAS | en-tx | Texas |
ESPERANTO | eo | Esperanto |
SPANISH | es | Spanish (español) |
ESTONIAN | et | Estonian (eesti keel) |
PERSIAN | fa | Persian (فارسی) |
FINNISH | fi | Finnish (suomi) |
FRENCH | fr | French (français) |
IRISH | ga | Irish (Gaeilge) |
GUJARATI | gj | Gujarati (ગુજરાતી) |
GALICIAN | gl | Galician (galego) |
HEBREW | he | Hebrew (עברית) |
HINDI | hi | Hindi (हिंदी) |
CROATIAN | hr | Croatian (hrvatski) |
CREOLE | ht | Creole (kreyòl) |
HUNGARIAN | hu | Hungarian (magyar) |
INDONESIAN | id | Indonesian (Bahasa Indonesia) |
ICELANDIC | is | Icelandic (Íslenska) |
ITALIAN | it | Italian (italiano) |
JAPANESE | ja | Japanese (日本語) |
JAVANESE | jv | Javanese (Basa Jawa) |
GEORGIAN | ka | Georgian (ქართული) |
KANNADA | kn | Kannada (ಕನ್ನಡ) |
KOREAN | ko | Korean (한국어) |
LITHUANIAN | lt | Lithuanian (lietuvių kalba) |
LUXEMBURGISH | lu | Luxemburgish (Lëtzebuergesch) |
LATVIAN | lv | Latvian (latviešu) |
MACEDONIAN_CYRILLIC | mk-Cyrl | Macedonian (Македонски) |
MACEDONIAN_LATIN | mk-Latn | Macedonian (Latin) (Makedonski (Latinica)) |
MALAYALAM | ml | Malayalam (മലയാളം) |
MONGOLIAN | mn | Mongolian (монгол) |
MARATHI | mr | Marathi (मराठी) |
NEPALI | ne | Nepali (नेपाली) |
DUTCH | nl | Dutch (Nederlands) |
NORWEGIAN | no | Norwegian (norsk) |
PANJABI | pa | Panjabi (ਪੰਜਾਬੀ) |
POLISH | pl | Polish (polski) |
PORTUGUESE | pt | Portuguese (português) |
ROMANIAN | ro | Romanian (română) |
RUSSIAN | ru | Russian (русский) |
SLOVAK | sk | Slovak (Slovensky) |
SLOVENIAN | sl | Slovenian (Slovenski) |
SERBIAN_CYRILLIC | sr-Cyrl | Serbian (Српски) |
SERBIAN_LATIN | sr-Latn | Serbian (Latin) (Srpski (Latinica)) |
SWEDISH | sv | Swedish (Svenska) |
TAMIL | ta | Tamil (தமிழ்) |
TELUGU | te | Telugu (తెలుగు) |
THAI | th | Thai (ไทย) |
KLINGON | tlh | Klingon (tlhIngan) |
TURKISH | tr | Turkish (Türkçe) |
TATAR | tt | Tatar (Татарча) |
UKRAINIAN | uk | Ukrainian (Українська) |
URDU | ur | Urdu (اردو) |
UZBEK | uz | Uzbek (Узбекча) |
VIETNAMESE | vi | Vietnamese (Tiếng Việt) |
CHINESE_SIMPLIFIED | zh-CN | Chinese simplified (简体中文) |
CHINESE_TRADITIONAL | zh-TW | Chinese traditional (繁體中文) |