In the previous tutorial, I explained about the Testing of Web Application using Serenity with JUnit4. In this tutorial, I’ll explain the Integration of Serenity BDD with JUnit5.
Pre-Requisite
- Java 11 installed
- Maven installed
- Eclipse or IntelliJ installed
This framework consists of:
- Java 11
- Maven – 3.8.1
- Serenity – 3.2.0
- Serenity JUnit5 – 3.2.0
- JUnit5 – 5.8.0
- Maven Surefire Plugin – 3.0.0-M5
- Maven Failsafe Plugin – 3.0.0-M5
- Maven Compiler Plugin – 3.8.1
Implementation Steps
- Update Properties section in Maven pom.xml
- Add Serenity, JUnit Jupiter API, JUnit Jupiter Engine, and JUnit5 dependencies to POM.xml
- Update Build Section of pom.xml
- Create test code under src/test/java folder
- Create serenity.conf file under src/test/resources
- Create serenity.properties file at the root of the project
- Run the tests through command line which generates Serenity Reports
Structure of Project

Step 1 – Update Properties section in Maven pom.xml
<properties>
<serenity.version>3.2.0</serenity.version>
<junit5.version>5.8.2</junit5.version>
<maven.surefire.plugin.version>3.0.0-M5</maven.surefire.plugin.version>
<maven.failsafe.plugin.version>3.0.0-M5</maven.failsafe.plugin.version>
<maven.compiler.plugin.version>3.8.1</maven.compiler.plugin.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<encoding>UTF-8</encoding>
<tags></tags>
<webdriver.base.url></webdriver.base.url>
</properties>
Step 2 – Add Serenity and JUnit dependencies to POM.xml
<dependencies>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-core</artifactId>
<version>${serenity.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-junit5</artifactId>
<version>${serenity.version}</version>
<scope>test</scope>
</dependency>
<!-- JUNIT 5 DEPENDENCY-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit5.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit5.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
Step 3 – Update Build Section of pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven.surefire.plugin.version}</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${maven.failsafe.plugin.version}</version>
<configuration>
<includes>
<include>**/*Test.java</include>
<include>**/Test*.java</include>
<include>**/*TestSuite.java</include>
<include>**/When*.java</include>
</includes>
<systemPropertyVariables>
<webdriver.base.url>${webdriver.base.url}</webdriver.base.url>
<junit.jupiter.extensions.autodetection.enabled>true</junit.jupiter.extensions.autodetection.enabled>
</systemPropertyVariables>
</configuration>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>net.serenity-bdd.maven.plugins</groupId>
<artifactId>serenity-maven-plugin</artifactId>
<version>${serenity.version}</version>
<configuration>
<tags>${tags}</tags>
<reports>single-page-html</reports>
</configuration>
<executions>
<execution>
<id>serenity-reports</id>
<phase>post-integration-test</phase>
<goals>
<goal>aggregate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>net.serenity-bdd</groupId>
<artifactId>serenity-single-page-report</artifactId>
<version>${serenity.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Step 4 – Create Test Class sunder src/test/java folder
ApplicationLoginJUnit5Tests.java
import net.serenitybdd.core.Serenity;
import net.serenitybdd.junit5.SerenityJUnit5Extension;
import net.thucydides.core.annotations.Managed;
import net.thucydides.core.annotations.Steps;
import net.thucydides.core.annotations.Title;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.openqa.selenium.WebDriver;
import static org.assertj.core.api.Assertions.assertThat;
@ExtendWith(SerenityJUnit5Extension.class)
class ApplicationLoginJUnit5Tests {
@Managed
WebDriver driver;
@Steps
NavigateAction navigateAction;
@Steps
StepLoginPage loginPage;
@Steps
StepDashBoardPage dashboardPage;
@Steps
StepForgotPasswordPage forgetPasswordPage;
@Test
@Title("Login to application with valid credentials navigates to DashBoard page")
void successfulLogin() {
navigateAction.toTheHomePage();
// When
loginPage.inputUserName("Admin");
loginPage.inputPassword("admin123");
loginPage.clickLogin();
// Then
Serenity.reportThat("Passing valid credentials navigates to DashBoard page",
() -> assertThat(dashboardPage.getHeading()).isEqualToIgnoringCase("DashBoard"));
}
@Test
@Title("Login to application with invalid credential generates error message")
void unsuccessfulLogin() {
navigateAction.toTheHomePage();
// When
loginPage.inputUserName("Admin");
loginPage.inputPassword("admin1232");
loginPage.clickLogin();
// Then
Serenity.reportThat("Passing invalid credentials generates error message",
() -> assertThat(loginPage.loginPageErrorMessage()).isEqualToIgnoringCase("Invalid credentials"));
}
@Test
@Title("Verify Forgot your password link")
void clickForgetPasswordLink() {
// Given
navigateAction.toTheHomePage();
// When
loginPage.clickForgetPasswordLink();
// Then
Serenity.reportThat("Open Forget Password Page after clicking forget password link",
() -> assertThat(forgetPasswordPage.getHeadingForgetPasswordPage())
.isEqualToIgnoringCase("Forgot Your Password?"));
}
}
To run a JUnit5 test with Serenity BDD, simply add the annotation @net.serenitybdd.junit5.SerenityTest (instead of @org.junit.runner.RunWith(net.serenitybdd.junit.runners.SerenityRunner.class) for JUnit4.
@ExtendWith(SerenityJUnit5Extension.class)
@Test is imported from package:-
import org.junit.jupiter.api.Test;
StepDashboardPage
public class StepDashBoardPage extends PageObject {
@FindBy(xpath = "//*[@id='content']/div/div[1]/h1")
WebElementFacade dashboardPageTitle;
@Step("Heading of DashBoard Page")
public String getHeading() {
return dashboardPageTitle.getText();
}
}
StepForgetPasswordPage
public class StepForgotPasswordPage extends PageObject {
@FindBy(xpath = "//*[@id='content']/div[1]/div[2]/h1")
WebElementFacade forgetLink;
@Step("Verify Forget Password Page ")
public String getHeadingForgetPasswordPage() {
return forgetLink.getText();
}
}
StepLoginPage
public class StepLoginPage extends PageObject {
@FindBy(name = "txtUsername")
WebElementFacade username;
@FindBy(name = "txtPassword")
WebElementFacade txtPassword;
@FindBy(name = "Submit")
WebElementFacade submitButton;
@FindBy(id = "spanMessage")
WebElementFacade errorMessage;
@FindBy(xpath = "//*[@id='forgotPasswordLink']/a")
WebElementFacade forgotPasswordLinkText;
@Step("Enter Username")
public void inputUserName(String userName) {
$("[name='txtUsername']").sendKeys((userName));
}
@Step("Enter Password")
public void inputPassword(String passWord) {
txtPassword.sendKeys((passWord));
}
@Step("Click Submit Button")
public void clickLogin() {
submitButton.click();
}
@Step("Error Message on unsuccessful login")
public String loginPageErrorMessage() {
return errorMessage.getText();
}
@Step("Click Forget Password Link")
public void clickForgetPasswordLink() {
forgotPasswordLinkText.click();
}
}
The WebElementFacade class contains a convenient fluent API for dealing with web elements, providing some commonly-used extra features that are not provided out-of-the-box by the WebDriver API. WebElementFacades are largely interchangeable with WebElements: you just declare a variable of type WebElementFacade instead of type WebElement
The @Steps annotation marks a Serenity step library.
Create the test following the Given/When/Then pattern and using step methods from the step library.
The @Title annotation lets you provide your own title for this test in the test reports. Serenity @Title is considered for the Serenity report. Consistently with Junit4, the @Title
annotation does not influence the name in the Junit report.
The JUnit Serenity integration provides some special support for Serenity Page Objects. In particular, Serenity will automatically instantiate any PageObject fields in your JUnit test.
Junit5 @Disabled annotation can be used on test and step methods(same as @Ignore in JUnit4).
Step 5 – Create serenity.conf file under src/test/resources
Serenity.conf file is used to specify various features like the type of web driver used, various test environments, run tests in headless mode, and many more options.
webdriver {
driver = chrome
}
headless.mode = true
#
# Chrome options can be defined using the chrome.switches property
#
chrome.switches = """--start-maximized;--test-type;--no-sandbox;--ignore-certificate-errors;
--disable-popup-blocking;--disable-default-apps;--disable-extensions-file-access-check;
--incognito;--disable-infobars,--disable-gpu"""
pages {
loginForm = "https://opensource-demo.orangehrmlive.com/"
}
Step 6 – Create serenity.properties file at the root of the project
serenity.project.name = Serenity and JUnit5 Demo
Step 7 – Run the tests through the command line which generates Serenity Report
Execute the tests through the command line by using the below command
mvn clean verify
The output of the above test execution is

The path of Serenity’s reports is mentioned in the image. The reports are generated under /target/site/serenity/.
Index.html


The detailed steps of the tests can also be viewed in the Serenity Report. It shows the execution time of all the steps in a Test.

Serenity-Summary.html

We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!