Junit 5 Test Runner Configuration with Cucumber

Koushicksudharsanam
2 min readDec 23, 2023

In this Article we will be discussing about how to Configure and run the Cucumber Tests using Junit5.

Contents:

  1. Need of Junit5?
  2. What is the Architecture in Junit5?
  3. How to Configure your Cucumber tests using Junit5?

Need of Junit5?

The JUnit 5 version contains a number of exciting innovations, with the new features in Java 8 and above, as well as enabling many different styles of testing.

Furthermore, there’s now direct support to run Unit tests on the JUnit Platform in Eclipse, as well as IntelliJ. We can, of course, also run tests using the Maven Test.

What is the Architecture in Junit5?

JUnit 5 comprises the Architecture in three different sub-projects as below,

  1. Junit PlatForm = The platform is responsible for launching testing frameworks on the JVM
  2. Junit Jupiter = This module includes new programming and extension models for writing tests in JUnit 5
  3. Junit Vintage = The Vintage supports running tests based on JUnit 3 and JUnit 4 on the JUnit 5 platform.

How to Configure your Cucumber tests using Junit5?

In the Junit5, the Test Runner Class looks Totally Different but to Configure it using Junit5 , Firstly we need the below dependency

<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-suite</artifactId>
<version>${junitPlatformEngine}</version>
</dependency>

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumberJava}</version>
</dependency>

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit-platform-engine</artifactId>
<version>${cucumberJunit}</version>
</dependency>

Once the Above dependency is configured in the pom.xml, you need to configure the below annotation in Test Runner Class.

  1. Suite = it will marks a class as a test suite on the JUnit Platform
  2. IncludeEngines(“Cucumber”) = it specifies which TestEngines to be included when running a test suite on the JUnit Platform. In our case it is Cucumber
  3. SelectClasspathResource = It specifies where my Step def are located
  4. ConfigurationParameter = Using ConfigurationParameter we are calling the below parameters like we did in the CucumberOptions
Constants.FEATURES_PROPERTY_NAME
Constants.GLUE_PROPERTY_NAME
Constants.FILTER_TAGS_PROPERTY_NAME
Constants.EXECUTION_DRY_RUN_PROPERTY_NAME
Constants.PLUGIN_PROPERTY_NAME

So, now how the TestRunner looks in Junit5, here is the sample below

package com.koushick.BDD.TestRunner;

import io.cucumber.junit.platform.engine.Constants;
import org.junit.platform.suite.api.ConfigurationParameter;
import org.junit.platform.suite.api.IncludeEngines;
import org.junit.platform.suite.api.SelectClasspathResource;
import org.junit.platform.suite.api.Suite;

@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("com/koushick/BDD/Steps")
@ConfigurationParameter(key = Constants.FEATURES_PROPERTY_NAME,value = "src/test/resources/testcases/searchGoogle.feature")
@ConfigurationParameter(key = Constants.GLUE_PROPERTY_NAME,value = "com/koushick/BDD/Steps")
@ConfigurationParameter(key = Constants.FILTER_TAGS_PROPERTY_NAME,value = "@googleSearch")
@ConfigurationParameter(key = Constants.EXECUTION_DRY_RUN_PROPERTY_NAME,value = "false")
@ConfigurationParameter(key = Constants.PLUGIN_PROPERTY_NAME,value = "pretty, html:target/cucumber-report/cucumber.html")
public class PrimaryTestRunner {
}

Attaching the image for reference

Hope it helps!

Happy Learning! :)

--

--