About Vinod Kumar Kashyap

Vinod is Sun Certified and love to work in Java and related technologies. Having more than 10 years of experience, he had developed software’s including technologies like Java, Hibernate, Struts, Spring, HTML 5, jQuery, CSS, Web Services, MongoDB, AngularJS. He is also a JUG Leader of Chandigarh Java User Group.

JUnit Cucumber Example

In this example we shall show users the usage of Cucumber with JUnit. JUnit Cucumber example will follow a brief introduction about the relation they share and how we can use both with each other. This is a very basic example and users are recommended to test their own scenarios after reading this example.

After going through example you will be familiar with the uses of Cucumber. We shall show you the basic steps for creating and building an small example which which test your cases with JUnit and Cucumber.

If you are regular reader of my blogs you are already familiar with the JUnit. If not please go through some JUnit examples.

Want to be a JUnit Master ?

Subscribe to our newsletter and download the JUnit Programming Cookbook right now!

In order to help you master unit testing with JUnit, we have compiled a kick-ass guide with all the major JUnit features and use cases! Besides studying them online you may download the eBook in PDF format!

Email address:

Leave this field empty if you're human:

1. Introduction

Cucumber is a testing framework which supports Behaviour Driven Development (BDD). It lets us define application behaviour in plain meaningful English text using a simple grammar defined by a language called Gherkin. Cucumber itself is written inRuby, but it can be used to test code written in Ruby or other languages including but not limited to Java, C# and Python.

Cucumber is providing a way for non-technical person to define test cases for a product, and on the other hand, our expectation is for smooth and timely execution of such test cases.

Gherkin is the language that Cucumber understands. It is a Business Readable, Domain Specific Language that lets you describe software’s behaviour without detailing how that behaviour is implemented. See below how we can do this.

Given: The purpose of givens is to put the system in a known state before the user (or external system) starts interacting with the system (in the When steps).

When: The purpose of When steps is to describe the key action the user performs (or, using Robert C. Martin’s metaphor, the state transition).

Then: The purpose of Then steps is to observe outcomes. The observations should be related to the business value/benefit in your feature description.

Here we are asking Maven to fetch all jars related to the example. We have defined 3 jars and if there are any dependencies of these jars then they will be automatically pulled by Maven.
First at line no 5, is cucumber-junit jar, which is used with JUnit to test our example.
Second at line no 11, is junit jar, which is our main jar for testing.
Third at line no 18, is cucumber-java jar, that is used by our application and helps cucumber to recognize our Java syntax.
In line no 25,26 we have defined that maven should use 1.8 version of Java.

4.1 Model Class

Let’s start with a small model class. It is a simple class with 3 variables assigned to it and all will be used for testing. We will see the usage further in example.

4.2 Testing Classes

We will create 2 classes that are used by Cucumber for testing. First is feature file. This is a simple file which defines our cases i.e. Given, When and Then scenarios.
The extension for this file is .feature

user.feature

Feature: User Certification
Scenario: User is Passed
Given that the user Vinod is given a task to clear Java certification exam
When Vinod got 60 marks in exam
Then Vinod is known as Java certified

In this file we have defined some of the test scenarios. Let’s examine them.
Line no 1 specifies the name of our feature i.e. a name that can be used to identified the feature.
Line no 2 defines the Scenario. Here we are writing the name of the scenario that we want to test. In our case we are testing that User is certified.
Next 3 lines are self defined and explained above.

After writing the features that we want to test, we need to create the steps file that tell cucumber what exactly to be tested.

As you see that the class is annotated with the @RunWith(Cucumber.class) class. And one more thing that needs to be noted is that the class has nothing inside it. It is bare minimum class that helps in running our tests with the Cucumber.

When you run your test case by right clicking the above class and Run As -> JUnit test, then you will see the following output in the console and the JUnit window.

Output at console

1 Scenarios ( [32m1 passed [0m)
3 Steps ( [32m3 passed [0m)
0m0.129s

Output at JUnit window

Figure 3: JUnit Cucumber Example Output

5. Conclusion

In conclusion, we have seen how cucumber helps us to run the test cases with the natural language with plain english. We have also learned how we can test the cucumber with JUnit. Individually they are very good but when used together they will create a blast that helps the Java programmer to test his scenarios.

6. Download The Source Code

This is JUnit Cucumber Example

Download
You can download the full source code of this example here: JunitCucumber.zip

Leave a Reply

thanks for the article, very interesting.. However, i think we have to put the user.feature file in resources/junitcucumber, otherwise, it will return cucumber jvm CucumberException: No features found at []

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Receive Java & Developer job alerts in your Area

Leave this field empty if you're human:

Join Us

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.