cucumber junit-test-runner-class

Now that we have defined the test its time to run our test. But before we do that we have to add a class for running our tests. Cucumber uses Junit framework to run. As Cucumber uses Junit we need to have a Test Runner class. This class will use the Junit annotation @RunWith(), which tells JUnit what is the test runner class. It more like a starting point for Junit to start executing your tests. In the src folder create a class called TestRunner

JUnit Test Runner Class

Create a new Class file in the ‘cucumberTest‘ package and name it as ‘TestRunner‘, by right click on the Package and select New > Class. This class just need annotations to understand that cucumber features would be run through it and you can specify feature files to be picked up plus the steps package location. There are bunch of other parameters that it can take, to be discussed later in Cucumber Options.
package cucumberTest;

import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(
features = "Feature"
,glue={"stepDefinition"}
)

public class TestRunner {

}
For the curios minds, I will explain this code. Note that it is covered in details in coming tutorials. Consider this as a limited description.

Import Statements

First import statement ‘org.junit.runner.RunWith‘ imports @RunWith annotation from the Junit class. @RunWith annotation tells JUnit that tests should run using Cucumber class present in ‘Cucumber.api.junit‘ package.
Second import statement ‘cucumber.api.CucumberOptions‘ imports the @CucumberOptions annotation. This annotation tells Cucumber a lot of things like where to look for feature files, what reporting system to use and some other things also. But as of now in the above test we have just told it for the Feature file folder.


Run the Cucumber Test

Now we are all set to run the first Cucumber test. There are multiple ways and runners to use when it comes to cucumber feature files. We would try to understand how to run it from the IDE first and then from a command line at a later point.
Even from the IDE, there are a couple of ways to run these feature files.
  • Click on the Run button on eclipse and you have your test run
  • Right Click on TestRunner class and Click Run As  > JUnit Test Application
You will think where is the java code that will execute for these tests? Well don’t worry about that at this moment. Let’s just see what we have on the console window. Here is the text that I got on my console. Look how Cucumber has suggested that you should implement these methods so that the Steps mentioned in the Feature file can be traced to Java methods, which can be executed while executing the feature file.
First_Cucumber_Test_8

Now your project should look like this in Eclipse IDE:
First_Cucumber_Test_9

With this understanding let’s move on the next topic where we will talk about Gherkin Keywords and the syntax it provided to write application tests/behaviour.