DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • [DZone Survey] Share Your Expertise for Our Database Research, 2023 Edition
  • Java String Templates Today
  • A Data-Driven Approach to Application Modernization
  • What I Learned From Crawling 100+ Websites
  1. DZone
  2. Coding
  3. Frameworks
  4. An Alternative Approach of Writing JUnit Tests (the Jasmine Way)

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

Michael Scharhag user avatar by
Michael Scharhag
·
Jul. 12, 14 · Interview
Like (0)
Save
Tweet
Share
7.26K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Testing JUnit Jasmine (JavaScript testing framework)

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

Opinions expressed by DZone contributors are their own.

Trending

  • [DZone Survey] Share Your Expertise for Our Database Research, 2023 Edition
  • Java String Templates Today
  • A Data-Driven Approach to Application Modernization
  • What I Learned From Crawling 100+ Websites

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: