J8Spec is a library that allows tests written in Java to follow the BDD style introduced by RSpec and Jasmine.
If you are not familiar with those tools, you should definitely take some time to try them out.
Here is an example:
Since J8Spec comes with a JUnit runner, it works out-of-the-box with IDEs and build tools that support JUnit.
J8Spec is available in the Maven central repository. Just add it as a test dependency and you are good to go.
Here is a example of Gradle build file. You can find the entire sample project here.
Here is a example of Maven build file. You can find the entire sample project here.
Property | Description |
---|---|
j8spec.ci.mode | Intended to be used in the Continuous Integration environment to prevent accidental usage of xdescribe, xcontext, xit, fdescribe, fcontext, and fit. Also, it will not allow a hard-coded seed for the random order. Default value is false. |
j8spec.seed | Intended to be used while debugging a spec that fails in a particular random order. Not allowed when j8spec.ci.mode is true. |
j8spec.junit.description.format | When generating the each example description for JUnit, this format pattern is used. The first item is the description of all containers where the example is placed. The second is the example description. Default value is "%1$s/%2$s". See Format String Syntax for details. |
j8spec.junit.description.separator | When generating each example description for JUnit, this separator is used to compose the description of all container contexts where the example is placed. Default value is "/". |
J8Spec tries its best to be as similar as possible to RSpec and Jasmine, but it is still bound to the restrictions imposed by Java.
Where as in Ruby and JavaScript it is trivial to access a local variable within a closure, in Java, the local variable must be effectively* final - which means it cannot have its value changed.
*Since Java 8, the compiler can figure out if the variable is effectively final - no need for the final modifier. See Lambda Expressions.
One way to work around this is to use instance attributes.
Another way is using J8Spec's Var wrapper. It is a simple wrapper around the test subject. It allows the local variable to be effectively final whilst the internal reference can be modified.
Although it adds a little bit of noise to the code, it allows variables to be defined close to the context where it's being used.
Feedback is always welcome. Feel free to file a pull request or issue with the Github repository.