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

A Functional Approach to Given When Then Using Java 8

DZone's Guide to

A Functional Approach to Given When Then Using Java 8

A couple of months ago, while working on a new project, I started to look for an easy way to have this Given-When-Then separation in my tests.

· DevOps Zone
Free Resource

The Nexus Suite is uniquely architected for a DevOps native world and creates value early in the development pipeline, provides precise contextual controls at every phase, and accelerates DevOps innovation with automation you can trust. Read how in this ebook.

The Given-When-Then story

Given-When-Then started out as a pattern for writing tests, notably with BDD. The idea is to split the test into three parts:

  1. Given: The initial context is received.

  2. When: One or more actions are performed.

  3. Then: The action results are checked against expectations.

Several testing frameworks have used this approach for writing tests. One of these is Cucumber, where the test is written using human-readable sentences and the coding is done behind the scenes by implementing a fixture for each test step.  A very basic test would look like this:

Given a valid account
When you log in
Then you should see the dashboard

Usage in Code

At some point, some developers started to split their test implementations into the three Given-When-Then parts.

Some used comments:

@Test
public void regularUserShouldSeeDashboardWhenLoggedIn() {
    //Given
    User regularUser = new RegularUser("John");

    //When
    Application.login(regularUser);

    //Then
    assertTrue("dashboard should be visible", ApplicationSpy.isDashboardVisible());
}

And some used labels:

@Test
public void regularUserShouldSeeDashboardWhenLoggedIn() {
    Given:
    User regularUser = new RegularUser("John");

    When:
    Application.login(regularUser);

    Then:
    assertTrue("dashboard should be visible", ApplicationSpy.isDashboardVisible());
}

This separation makes the test easier to read no matter which of the above options you choose.

Richard Warburton wrote an article where he explored other ways to use labels for the Given-When-Then code separation.

A functional approach to Given When Then using Java 8

A couple of months ago, while working on a new project, I started to look for an easy way to have this Given-When-Then separation in my tests. This was a Java 8 project and I was sure that there was a new way of doing this using lambdas.

To my surprise, I didn't find anything revolutionary, so I decided to give 15 minutes to the crazy idea of doing something myself.

If you think about it, you should see that there is a chain in Given-When-Then. This is because values are passed from one step to the other:

  1. Given receives the context and passes it to When.

  2. When receives the context, performs one or more actions, and passes the result to Then.

  3. Then receives the result and checks it against the expectations.

So, then, why wouldn't we use chaining functions to represent the Given-When-Then parts in our code?

And so, using a bit of lambda magic, this has been made to work:

@Test
public void regularUserShouldSeeDashboardWhenLoggedIn() {
  given("a regular user", new RegularUser("John"))
    .when("logging in", regularUser -> Application.login(regularUser).getSpy())
    .then("dashboard should be visible", applicationSpy -> applicationSpy.isDashboardVisible());
}

The solution has been made public. Here is the GitHub Java 8 project.

The equivalent JavaScript solution was much easier to develop:

it('should display the dashboard when a regular user logs in', () => {
  given(getNewRegularUser())
    .when(regularUser => performLogin(regularUser).getSpy())
    .then(applicationSpy => applicationSpy.isDashboardVisible());
});

The JavaScript GitHub project can be found here.

Conclusion

This is just another way of doing the Given-When-Then separation in code. It works for me but it might not be the right choice for someone else. However, I think it's always nice to have options.

I would like to thank S&T for allowing the GitHub projects to start.

If you have any thoughts, please feel free to comment. Thank you!

The DevOps Zone is brought to you in partnership with Sonatype Nexus.  See how the Nexus platform infuses precise open source component intelligence into the DevOps pipeline early, everywhere, and at scale. Read how in this ebook

Topics:
java 8 ,javascript ,software testing ,devops ,cucumber

Published at DZone with permission of Gabriel Deliu. 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 }}