Thomas Sundberg

November 1, 2012

Cucumber-JVM – not just for testing GUIs

This post has been migrated to

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.

Table of content

BDD should be declarative

A lot of developers think of tests as unit tests. Tests written using tools like JUnit are almost always imperative. This is of course due to the language the tests are being written in. Java is an object oriented and imperative language. It is therefore natural that the tests will be imperative.

If you specify properties you want a system to have, then you will be using a declarative paradigm instead.

BDD and Cucumber should be used declarative. You define properties that should hold true for a solution. A set of boundaries the solution should stay within. You don’t know, and should not care, about the implementation when you define your features. The implementation comes later and is an S.E.P. or ‘Somebody Else’s Problem’. You might be somebody else, but then you act in a different role that you would like to separate from the role that defines the wish list (requirements) for the system.

This is similar to SQL (which is an implementation of Relational Calculus and Tuple Calculus). You define properties that you expect the answer to have and don’t really care about how it is calculated. You might care if you are trying to apply some optimization, but that’s an implementation detail and really not important for the proper functionality.

The Example

Finding good examples that show the problem you want to highlight without being trivial is difficult. Understanding the large picture may be hard if there are too many details. Another problem one faces when writing a tutorial is to use either too large or too small steps. If you take too large steps without explanations, then people will have problems following your ideas. Too small steps tend to be boring and readers may feel that you assume that they are stupid. Finding the right balance is crucial and really difficult.

I have used an example of fuelling a car in previous examples. I could reuse it, but I have decided that a car rental system would be more fun. It is the same thing from a tool perspective. Things are created, located, updated, and removed. The important thing to understand is that the example, or the model used in the example, is not important. It is just at tool for describing the ideas.

In a car hiring system you would expect to be able to

  • Search for cars to rent
  • Rent a car
  • Return a car
  • Charge rent for a car
  • Maintain an inventory of cars

The most valuable feature for the company that uses a system like this might be the possibility to have its customers renting cars. This is how they making there living.

Some infrastructure is needed so cars can be rented. The cars have to be created in the system, it must be possible to calculate a price for renting a car and similar. This list could be very long. I will start with perhaps the most important feature; find and rent a car. If I can deliver this feature early, real world users can try it out and give me feedback if I am missing something important.

This is actually the only feature I will implement. It will be implemented in such a way that I hope you are able to understand how it is done and understand how the example can be expanded for the second most important feature, without breaking the first feature.

I will use Model View Controller, MVC, strict. The model will be developed first and it will never change when the view is changed. The reason for this is simple; the desired behaviour is implemented in the model. Since this is the behaviour we want, we don’t have any valid reason to change it just because an interface is added.

Next -Building the model


  1. nice article!

    have you tested concordion ( or graphwalker ( and do you have any comments on pros and cons in comparison to those tools?


    Comment by Andreas — November 1, 2012 @ 12:48

    • Hi!

      I have not tested GraphWalker. My opinion about Concordion is positive, but Concordion is more technical compared to Cucumber. More technical is in some cases not positive. Especially the day when you get your product owner to write the features.


      Comment by Thomas Sundberg — November 1, 2012 @ 14:42

  2. Great post! I have read about BDD, cucumber and SbE and this post is by far the best.

    I’ve just missed some DSL like Cabybara. Have you used it?

    Comment by leoalfabr — December 18, 2012 @ 20:59

  3. Reblogged this on ChromeHat and commented:
    BDD and Cucumber

    Comment by Modus Operandi — February 6, 2013 @ 22:19

  4. Great post! I had added maven cucumber dependency in pom.xml and trying to create a feature file and step defnitions in java.But i dont find help anywhere for cucumber jvm.
    If you can , please help

    Comment by sindhuri — March 27, 2013 @ 01:59

    • Hi!

      To be able to assist you I would need more information. such as the error you encountered. If I do some guessing I would ask, did you add the dependency in the right pom? There are more than one pom involved in this example. Double check where you have added the Cucumber-jvm references.


      Comment by Thomas Sundberg — March 27, 2013 @ 11:03

  5. Thank you very much!

    Comment by Isabek — July 31, 2013 @ 21:45

  6. […] information about that is distributed between various samples and blog posts but there’s no dedicated page showing all steps in […]

    Pingback by JBehave vs Cucumber JVM comparison | Myillusions — February 17, 2014 @ 14:28

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at

%d bloggers like this: