This is the example I implemented at JDD 2014 in Krakow, Poland at my Cucumber-JVM tutorial.
It is a (baby) step by step tutorial. The purpose for me taking baby steps is that you should be able to follow and implement the same things. Be prepared to spend some time with the implementation, it will probably take you a few hours.
Before we dive into the example, let me define what I am aiming for. My goal is to show you how an example (or specification if you want) can be executed. The example is written in plain text and is used to automate the testing of the system I will create. These executable examples can later be relied upon for regression testing and a living documentation.
Cucumber is very easy to run from Maven. How do you run it from Gradle?
The answer to this question is: It is as easy as to run it from Maven. Depending on your background, perhaps even easier. The reason for this is because we run Cucumber from a JUnit runner. That is as a unit test with a specific runner. Maven or Gradle really doesn’t have anything to do with this. It all boils down to the need for a Gradle project that can build a Java project. A Java project that has a unit test.
Cucumber has a nice feature that will help you to use tables in your scenarios. The table can easily be converted to a list or a map that you can use in your step. I will show you a few examples that may help you get started.
This is the example I showed at the I T.A.K.E. Unconference 2014 in Bucharest. I created it for your convenience so you should be able to implement it yourself after the presentation.
Before we dive into the example, let me just recap what I am aiming for. I will show you how an example (or specification if you want) can be executed. The example is written in plain text and it is used as the basis for an execution. This example can later be relied upon for regression testing as well as living documentation.
Before I can try to motivate why you should use a tool, let me define what it is and what it does.
What is cucumber?
Cucumber is a tool for collaboration and testing. It is used to create examples of behaviour that are executable. Creating examples in a collaborative way emphasize close cooperation between business analysts, testers and developers. The examples they come up with can be used as acceptance tests for the system being developed. It can be used as a testing tool where the tests are defined in a business friendly language while still being executable.
Our goal is to create a common understanding of the problem and therefore simplify the communication between all parties involved. We would also like to get something that is possible to use for automating the verification of the resulting program. That is, use as a base for test automation of the system.
Cucumber is a tool that supports Behaviour Driven Development, BDD. A lot of people think that the only place where a system has behaviour is in the user interface and especially in the graphical user interface. As a developer I know that this is not the case. All systems have behaviour at different places and different levels.
I will show an example of how a system can be developed using its desired behaviour and start from a non graphical point. I will work from the model down to the database and when I’m happy with the logical behaviour I will add a graphical user interface on top of it. I will actually add a few different interfaces; two web-based, one swing and two different types of web services. The result will be an example of Model View Controller, MVC, developed using BDD.
An important point when I add the GUIs or web services is that I will not change the desired behaviour. I will only change how the behaviour is verified. This is one way of showing you that Cucumber and BDD is not about testing GUIs. It is about systems behaviour.
Previous – Introduction
The feature I will start with looks like this:
Feature: Rental cars should be possible to rent to gain revenue to the rental company.
As an owner of a car rental company
I want to make cars available for renting
So I can make money
Scenario: Find and rent a car
Given there are 18 cars available for rental
When I rent one
Then there will only be 17 cars available for rental
It consists of three parts:
- Given – the preconditions of the system under test. The setup of the systems state if you want. In this case make 18 compact cars available for rental in the system.
- When – the actual change of the system. Transforming it from the initial state to the final state. Rent one car.
- Then – the expected final state of the system. The verification that the state change was the desired change. After one car is rented, there should only be 17 left to rent.
Previous – Building the model
Many modern applications are built as web applications. The benefits are obvious, you don’t need to package your software in shrink-wrap and send it to your customers. Upgrading is easy, you have to upgrade the server you host the system on and that’s it.
The first user interface I will add to the rental system will therefore be a web GUI. It will be the simplest possible solution and the goal is not to build a fancy web app. The goal is to show how Cucumber can control a tool like Selenium WebDriver to assert the behaviour of the web application.
Previous – A JSF web application
A wicket application is yet another web application. I divide the project in two parts as earlier. The only large difference is the support class that will connect to the system under test. It has been adapted for another web application.
Previous – A Wicket web application
A Java Swing application is yet another graphical user interface that can be attached on top of the model developed earlier. The project is divided in the same way as earlier, in two parts. The only large difference here is the support class. It need to be adapted for a Swing user interface. Another difference is of obviously that the GUI is developed using Swing. But that has actually a rather small impact on the verification.