Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

An Alternative Approach of Writing JUnit Tests (the Jasmine Way)

DZone's Guide to

An Alternative Approach of Writing JUnit Tests (the Jasmine Way)

· DevOps Zone
Free Resource

The DevOps Zone is brought to you in partnership with Sonatype Nexus. The Nexus Suite helps scale your DevOps delivery with continuous component intelligence integrated into development tools, including Eclipse, IntelliJ, Jenkins, Bamboo, SonarQube and more. Schedule a demo today

Recently I wrote a lot of Jasmine tests for a small personal project. It took me some time until I finally got the feeling of getting the tests right. After this, I always have a hard time when switching back to JUnit tests. For some reason JUnit tests did no longer feel that good and I wondered if it would be possible to write JUnit tests in a way similar to Jasmine.

Jasmine is a popular Behavior Driven Development testing framework for JavaScript that is inspired by RSpec (a Ruby BDD testing Framework).

A simple Jasmine test looks like this:

describe('AudioPlayer tests', function() {
var player;
beforeEach(function() {
player = new AudioPlayer();
});
it('should not play any track after initialization', function() {
expect(player.isPlaying()).toBeFalsy();
});
...
});

The describe() function call in the first line creates a new test suite using the description AudioPlayer tests. Inside a test suite we can use it() to create tests (called specs in Jasmine). Here, we check if theisPlaying() method of AudioPlayer returns false after creating a new AudioPlayer instance.
The same test written in JUnit would look like this:

public class AudioPlayerTest {
private AudioPlayer audioPlayer;
@Before 
public void before() {
audioPlayer = new AudioPlayer();
}
@Test
void notPlayingAfterInitialization() {
assertFalse(audioPlayer.isPlaying());
}
...
}

Personally I find the Jasmine test much more readable compared to the JUnit version. In Jasmine the only noise that does not contribute anything to the test are the braces and the function keyword. Everything else contains some useful information.
When reading the JUnit test we can ignore keywords like void, access modifiers (private, public, ..), annotations and irrelevant method names (like the name of the method annotated with @Before). In addition to that, test descriptions encoded in camel case method names are not that great to read.

Besides increased readability I really like Jasmine's ability of nesting test suites.
Let's look at an example that is a bit longer:

describe('AudioPlayers tests', function() {
var player;
beforeEach(function() {
player = new AudioPlayer();
});
describe('when a track is played', function() {
var track;
beforeEach(function() {
track = new Track('foo/bar.mp3')
player.play(track);
});
it('is playing a track', function() {
expect(player.isPlaying()).toBeTruthy();
});
it('returns the track that is currently played', function() {
expect(player.getCurrentTrack()).toEqual(track);
});
});
...
});

Here we create a sub test suite that is responsible for testing the behavior when a Track is played byAudioPlayer. The inner beforeEach() call is used to set up a common precondition for all tests inside the sub test suite.

In contrast, sharing common preconditions for multiple (but not all) tests in JUnit can become cumbersome sometimes. Of course duplicating the setup code in tests is bad, so we create extra methods for this. To share data between setup and test methods (like the track variable in the example above) we then have to use member variables (with a much larger scope).
 Additionally we should make sure to group tests with similar preconditions together to avoid the need of reading the whole test class to find all relevant tests for a certain situation. Or we can split things up into multiple smaller classes. But then we might have to share setup code between these classes... 

If we look at Jasmine tests we see that the structure is defined by calling global functions (like describe(),it(), ...) and passing descriptive strings and anonymous functions.

With Java 8 we got Lambdas, so we can do the same right?
Yes, we can write something like this in Java 8:

public class AudioPlayerTest {
private AudioPlayer player;
public AudioPlayerTest() {
describe("AudioPlayer tests", () -> {
beforeEach(() -> {
player = new AudioPlayer();
});
it("should not play any track after initialization", () -> {
expect(player.isPlaying()).toBeFalsy();
});
});
}
}

If we assume for a moment that describe(), beforeEach(), it() and expect() are statically imported methods that take appropriate parameters, this would at least compile. But how should we run this kind of test?

For interest I tried to integrate this with JUnit and it turned out that this actually very easy (I will write about this in the future). The result so far is a small library called Oleaster.

A test written with Oleaster looks like this:

import static com.mscharhag.oleaster.runner.StaticRunnerSupport.*;
...
@RunWith(OleasterRunner.class)
public class AudioPlayerTest {
private AudioPlayer player;
{
describe("AudioPlayer tests", () -> {
beforeEach(() -> {
player = new AudioPlayer();
});
it("should not play any track after initialization", () -> {
assertFalse(player.isPlaying());
});
});
}
}

Only a few things changed compared to the previous example. Here, the test class is annotated with the JUnit@RunWith annotation. This tells JUnit to use Oleaster when running this test class. The static import ofStaticRunnerSupport.* gives direct access to static Oleaster methods like describe() or it(). Also note that the constructor was replaced by an instance initializer and the Jasmine like matcher is replaced with by a standard JUnit assertion.

There is actually one thing that is not so great compared to original Jasmine tests. It is the fact that in Java a variable needs to be effectively final to use it inside a lambda expression. This means that the following piece of code does not compile:

describe("AudioPlayer tests", () -> {
AudioPlayer player;
beforeEach(() -> {
player = new AudioPlayer();
});
...
});

The assignment to player inside the beforeEach() lambda expression will not compile (because player is not effectively final). In Java we have to use instance fields in situations like this (like shown in the example above).

In case you worry about reporting: Oleaster is only responsible for collecting test cases and running them. The whole reporting is still done by JUnit. So Oleaster should cause no problems with tools and libraries that make use of JUnit reports.

For example the following screenshot shows the result of a failed Oleaster test in IntelliJ IDEA:


If you wonder how Oleaster tests look in practice you can have a look at the tests for Oleaster (which are written in Oleaster itself). You can find the GitHub test directory here

Feel free to add any kind of feedback by commenting to this post or by creating a GitHub issue.

The DevOps Zone is brought to you in partnership with Sonatype Nexus. Use the Nexus Suite to automate your software supply chain and ensure you're using the highest quality open source components at every step of the development lifecycle. Get Nexus today

Topics:

Published at DZone with permission of Michael Scharhag, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}