Skip to main content

Cucumber with Java: JUnit Test Runner Class

Once you have defined your test cases and associated step definitions, you can run your test cases using several methods. One way to achieve this is using JUnit Test Runner Class.

For this we need to create new class which will use JUnit annotation @RunWith(), which tells JUnit that current class is the test runner class. When a class is annotated with @RunWith or extends a class annotated with @RunWith, JUnit will invoke the class it references to run the tests in that class instead of the runner built into JUnit.

@RunWith annotation tells JUnit that test should run using cucumber class which is present in io.cucumber.junit.Cucumber package.

Create a new class with name Runner.java, and paste below code.

package com.pb.cucumbertest.stepdefinitions;

import org.junit.runner.RunWith;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)
@CucumberOptions(
		plugin = {"pretty", "html:target/cucumber"},
		features = {"features"},
		glue={"com.pb.cucumbertest.stepdefinitions"},
		monochrome = true,
		tags = {"@contactus"},
		dryRun = false,
		strict = true
		)

public class Runner {

}

Give path of your feature file in "features" and package name of your step definitions in "glue". @CucumberOptions will be explained in more detail in next tutorial.

Next step is right click on your runner class and execute it using Run As->JUnit Test.

You can also run it other eclipse IDE feature like run button on top.

Video Tutorial

About

Elix is a premium wordpress theme for portfolio, freelancer, design agencies and a wide range of other design institutions.