Before you can write BDD tests, you probably need to know what they are. Short answer – BDD tests try to black-box test something according to its behaviour. One of the most common languages is Gherkin. In principle, Gherkin is a natural language based technique for describing feature and scenarios in terms of how they appear to an observer. You’ll know it’s Gherkin if you see a lot of:
- Feature – to describe a capability
- Scenario – to describe an example of a feature
- Given – setup
- When – execution
- Then – expectations
Other BDD specification languages also exist. RSpec style test frameworks tend to use:
- Describe – to describe a capability in terms of its behaviour
- It – to describe a scenario
- Expect – to verify things during testing that scenario
This post is concerned with Java and JVM. There are a cavalcade of possible tools you can use for BDD testing on the JVM. Here’s a quick round-up of tools I’ve heard of:
- Cucumber JVM – one of the leading products – based on Gherkin – uses plaintext feature files and wires up test code using reflection and regular expressions
- JBehave – also based on Gherkin – similar feature set to Cucumber JVM
- Spock – this relies on Groovy and feels like a blend of code and script – it has Gherkin-like when and then labels.
- Oleaster – this uses RSpec/Jasmine like syntax and requires Java 8. It includes a port of the “expect” framework you’d find in Jasmine.
- Spectrum – this is intended to be a Polyglot and Principle of Least Surprise framework. It uses RSpec/Jasmine syntax and also supports Gherkin syntax, all expressed in Java 8. No expectations framework is supplied as you can take your pick of JUnit, AssertJ, Hamcrest‘s ones as you prefer.
- Ginkgo4j – a port of Ginkgo to Java, including direct support for Spring. Ginkgo seems to be very similar to RSpec in its syntax.
- JGiven – this takes a completely different approach, encouraging you to create your own DSL for using in the tests.
- ColaTests – a Gherkin based JUnit runner where you write the steps and tests directly in the test class in Java annotated with Gherkin syntax.
- Specsy – intended for Scala, this can also be used a Java 8 lambda-based test framework, with examples and support for Groovy and Scala to boot. It’s very lightweight and hugely supports parallel testing and control of sharing state between tests.
The above is intended to be a rundown of tools that are out there – please comment with any omissions or errors and I will try to update the list.
Full disclosure – I’ve contributed to the Spectrum framework.