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.
Join the DZone community and get the full member experience.
Join For FreeThe 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:
Given: The initial context is received.
When: One or more actions are performed.
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:
Given receives the context and passes it to When.
When receives the context, performs one or more actions, and passes the result to Then.
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!
Published at DZone with permission of Gabriel Deliu. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments