2

Java developer testing toolbox

JBehave : code
Bookmark this on Hatena Bookmark
Share on LinkedIn
Pocket

An article dealing with 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 equiped 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 months.

testing :

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

Sometimes I have been using https://github.com/Pragmatists/JUnitParams to setup parameterized test easily.

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

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

Mocking

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

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

Asserts

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

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

BDD/

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

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

Take for example the framework SPOCK :

Documentation is there :

package condition

import spock.lang.Specification

class BasicsSpec extends Specification {
    def "withdraw some amount"() {
        given:
        def account = new Account(5.0)

        when:
        account.withdraw(2.0)

        then:
        account.balance == 3.0
    }
}

Looks great isn’t it ? It provides an canevas to write your tests and Groovy langage is considerably shortening your code.

There are plenty of other frameworks :

JBehave : code

JBehave : 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 tidious 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 {

    @ClassRule
    public static final JGivenClassRule writerRule = new JGivenClassRule();

    @Rule
    public final JGivenMethodRule scenarioRule = new JGivenMethodRule();

    @ScenarioStage
    GivenIngredients someState;

    @ScenarioStage
    WhenCook someAction;

    @ScenarioStage
    ThenSomeOutcome someOutcome;

    @Test
    public void cooking_blinis() {

        given().the_ingredient("egg");
        given().the_ingredient("flour");
        given().the_ingredient("milk");
        given().comment("We prepare the recipe");
        when().mixing();
        when().frying_in_hot_pane();
        then().we_have_blinis();
    }

    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 to this extension at the expense of a slower startup time for your tests.

Jnario

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

Data Randomizer

I become lazier with age and experience to produce stubs for my unit tests. I recommend to think as soon as possible how to collect your DTO and other value objects directly from your test environments and store them into 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 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 ha been open-sourced since.
  • Evosuite  : generate a suite of unit tests for a method. Check the screencast for morei nformations
  • Cucumber : https://.io/docs/reference/jvm
  • DBUnit : Write easier tests using your database.
  • HavaRunner : JUnit extension to execute concurrent test inside a same class
  • Randoop : Randomly generated tests.
  • Mock mail smtp server : useful library when you are coding unit tests for mail functionalities.

 

References :

Sylvain Leroy

Digital Solution Architect in Switzerland, I have previously created my own company, Tocea, in Software Quality Assurance. Currently living in Lausanne (CH)

2 Comments

  1. Nice article.

    Talking about assertions, we can also mention FEST assertions which provides a nice fluent API.

    As for BDD frameworks, after having experienced both JBehave and JGiven, i must say that JGiven is my first choice.
    Even if JBehave’s first intention was to let the opportunity to business analysts to write the scenario (plain text format, natural language), this task is often done by the devs themselves. On the other hand, JGiven assumes that scenarios should be written by devs (which doesn’t scenario to be thought by both BA and devs), but in the same time preserves the natural/domain language thanks to a DSL composed of a corpus of Given/When/Then steps. This choice avoid the devs to write the fixture code that was required by JBehave. Besides, JGiven offer much more features in terms of reporting and scenarios organization (see tags section in JGiven reference documentation). JBehave can also prouce nice reports when associated to the Serenity (Thucydides) framework but stay a level below (IMHO).

    • Thank you for this interesting feedback.

      Indeed I missed FEST assertions, I could not find them by querying on internet (Hamcrest alternatives) and two other frameworks in my review :
      – Feign REST Client
      – Wiremock

      These are two handy tools to create REST API and to mock your endpoints.

      I agree with you with your observation on JBehave, for the reason most of the functional work is exposed now as Endpoints and the BA are using Postman and other tools to test our code.
      The same reasoning can be applied to Fitness ( when did I have the need to test a function by BA?).

      I wrote two usecases (sample one and a real one) using JGiven. And I have finding the same benefits as using Spock and in addition I can keep my code in Java.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *