JBehave : code

Java developer testing toolbox


An article dealing with Java application and testing frameworks and related libraries at the heart of DevOps and more basically the Software Development Process & Assurance, testing, and monitoring tools are key tools to ensure a high level of quality and trust in your deliveries.

The JEE ecosystem is rather well equipped with a huge galaxy of tools. I offer you here my list of favorite tools.


Testing :

The collection of testing tools is amazingly vast and I discover some almost every month.

Unit testing :

I have been a practitioner of JUnit for a long time despite having been tempted once or twice by TestNG features (Parameterized test and test suites were yummy).

Sometimes I have been using https://github.com/Pragmatists/JUnitParams to set up parameterized tests easily.

And http://labs.carrotsearch.com/junit-benchmarks-tutorial.html to set up simple micro-benchmarks (yeah I know it exists JMH).

Don’t miss the new version 5 of JUnit, there are many cool features!


I am also a huge adept at Mockito. Plenty of other frameworks exists, a little bit more powerful (Powermock hum) but I stick to Mockito

Here also check the new features coming with Mockito 2.0, the fluent BDD interface is great.


You cannot avoid Hamcrest implementing a little bit more evolved assertions and get better-debugging messages.

I dream to have an equivalent version of Chai for Java 🙁


Many frameworks are available. I often suggest giving a try to write your unit tests in a language different from Java.

First, you may have the surprise of how much your productivity will increase, and some test frameworks are just great.

Take for example the framework SPOCK :

Next to read  Writing custom Cobol Rules with SonarQube

Documentation is there :

package condition

import .lang.Specification
class BasicsSpec extends Specification {
    def "withdraw some amount"() {
        def account = new Account(5.0)


        account.balance == 3.0

Looks great, isn’t it? It provides a canvas to write your tests and Groovy language is considerably shortening your code.

There are plenty of other frameworks :

JBehave : testing code
JBehave: testing code


JGiven framework is interesting since it provides a fluent API to write your tests.

As a daily practice, writing tests can become boring or tedious leading to a huge amount of duplicated codes. JGiven is offering to generate for you a fluent API.

The user guide is there.

public class CoookingBddTest {

    public static final JGivenClassRule writerRule = new JGivenClassRule();

    public final JGivenMethodRule scenarioRule = new JGivenMethodRule();

    GivenIngredients someState;

    WhenCook someAction;

    ThenSomeOutcome someOutcome;

    public void cooking_blinis() {

        given().comment("We prepare the recipe");

    private GivenIngredients given() {
        return this.someState.given();

    private ThenSomeOutcome then() {
        return this.someOutcome.then();

    private WhenCook when() {
        return this.someAction.when();

You should definitely have a look at this extension at the expense of a slower startup time for your tests.


JNario is an interesting however I do really hate Eclipse as XTend, it tends not to be working at all outside Eclipse. A definitely no-go for me.

Next to read  Spring Boot and Liquidbase : how to change the default path and filename

Data Randomizer

I become lazier with age and experience to produce stubs for my unit tests. I recommend thinking as soon as possible about how to collect your DTO and other valuable objects directly from your test environments and store them in your project.

At least you will have true test data to test your programs.

If you have forgotten to capture these data, these two libraries may help you :

  • Random beans: take any Java class, initialize it and fill it with random data
  • JFairy provides a random set of data for common information (Creditcard, IBAN, Name, Lastname, Firstname, etc)

Other interesting tools

I have tried a few tools to generate unit tests :

  • CodePro is an Eclipse plugin from a society acquired by Google. Their tool has been open-sourced since.
  • Evosuite: generate a suite of unit tests for a method. Check the screencast for more information.
  • Cucumber: https://cucumber.io/docs/reference/jvm
  • DBUnit: Write easier tests using your database.
  • HavaRunner: extension to execute concurrent test inside the same class
  • Randoop: Randomly generated tests.
  • Mock mail SMTP server: useful library when you are coding unit tests for mail functionalities.


References :

Sylvain Leroy

Senior Software Quality Manager and Solution Architect in Switzerland, I have previously created my own company, Tocea, in Software Quality Assurance. Now I am offering my knowledge and services in a small IT Consulting company : Byoskill and a website www.byoskill.com Currently living in Lausanne (CH)

View all posts by Sylvain Leroy →