Over a million developers have joined DZone.

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

Download the blueprint that can take a company of any maturity level all the way up to enterprise-scale continuous delivery using a combination of Automic Release Automation, Automic’s 20+ years of business automation experience, and the proven tools and practices the company is already leveraging.

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:

public void regularUserShouldSeeDashboardWhenLoggedIn() {
    User regularUser = new RegularUser("John");


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

And some used labels:

public void regularUserShouldSeeDashboardWhenLoggedIn() {
    User regularUser = new RegularUser("John");


    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:

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', () => {
    .when(regularUser => performLogin(regularUser).getSpy())
    .then(applicationSpy => applicationSpy.isDashboardVisible());

The JavaScript GitHub project can be found here.


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!

Download the ‘Practical Blueprint to Continuous Delivery’ to learn how Automic Release Automation can help you begin or continue your company’s digital transformation.

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.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}