Jasmine BDD style testing for Java

During the last six months or so testing has become a special interest of mine. Most of my career I have worked in the Java ecosystem and the majority of tests I’ve seen and written have been using JUnit. Currently I’m looking into the Javascript world, where testing often is done in a different way. In contrast to Java, BDD-style tests are much more common here. There are some libraries that extend JUnit so that you can write tests in a different way, too, JBehave for example enables you to use Gherkin as a specification language, for which you then write a Java adapter that actually implements those specifications.

There’s a number of different testing frameworks for Javascript, Mocha and Jasmine being used widely. Jasmine is a complete package containing libraries for mocking and for assertions. Mocha is smaller but if you choose additional libraries for the missing parts it can do the same things that Jasmine does and is more flexible. The type of tests I’m talking about look like this:

describe("A suite", function() {
  it("contains a specification with an expectation", function() {
    expect(true).toBe(true);
  });
})

While looking into Jasmine, I’ve gotten to know a library called Oleaster. It provides a test runner that integrates with JUnit and through a number of static methods it enables you to write tests the same way you would using Jasmine. A test written with Oleaster looks like this:

import static com.mscharhag.oleaster.matcher.Matchers.expect;
import static com.mscharhag.oleaster.runner.StaticRunnerSupport.beforeEach;
import static com.mscharhag.oleaster.runner.StaticRunnerSupport.describe;
import static com.mscharhag.oleaster.runner.StaticRunnerSupport.it;

import org.junit.runner.RunWith;

import com.mscharhag.oleaster.runner.OleasterRunner;

@RunWith(OleasterRunner.class)
public class HelloOleasterTest {

  private HelloOleaster helloOleaster;

  {
    beforeEach(() -> {
      helloOleaster = new HelloOleaster();
    });

    describe("When HelloOleaster is called", () -> {

      it("should return 'Hello World'", () -> {
        expect(helloOleaster.hello()).toEqual("Hello World!");
      });

    });
  }
}

The test specifications are contained in a code block that gets executed every time an instance of the test class is created. As you can see, Oleaster uses Lambdas to enable this style of testing. The describe() and it() functions basically register invocations that get exectuted by the test runner. That’s a lot less boilerplate code than you end up with conventional JUnit tests. Also, since Oleaster integrates with JUnit, it plays nicely with your IDE, too.

IDE Integration

Doesn’t that read better than those JUnit tests you know? You can use Oleaster by adding two additional dependencies to your project.

<dependency>
    <groupId>com.mscharhag.oleaster</groupId>
    <artifactId>oleaster-matcher</artifactId>
    <version>0.1.2</version>
</dependency>

<dependency>
    <groupId>com.mscharhag.oleaster</groupId>
    <artifactId>oleaster-runner</artifactId>
    <version>0.1.2</version>
</dependency>

If you like what you see you can read more about Oleaster in a blog post of the author.

One thought on “Jasmine BDD style testing for Java

Leave a Reply