[New post] Integration Testing of Springboot with Cucumber and TestNG
vibssingh posted: " HOME In this tutorial, I am going to build an automation framework to test Springboot application with Cucumber, Rest Assured and TestNG. What is Springboot? Spring Boot is an open-source micro framework maintained by a company called " QA Automation Expert
In this tutorial, I am going to build an automation framework to test Springboot application with Cucumber, Rest Assured and TestNG.
What is Springboot?
Spring Boot is an open-source micro framework maintained by a company called Pivotal. It provides Java developers with a platform to get started with an auto configurable production-grade Spring application. With it, developers can get started quickly without losing time on preparing and configuring their Spring application.
What is Cucumber?
Cucumber is a software tool that supports behavior-driven development (BDD). Cucumber can be defined as a testing framework, driven by plain English. It serves as documentation, automated tests, and a development aid – all in one.
This framework consists of:
Springboot – 2.5.2
Cucumber – 7.3.4
Java 11
TestNG – 7.3.4
Maven – 3.8.1
RestAssured – 5.1.1
Steps to setup Cucumber Test Automation Framework for API Testing using Rest-Assured
Add SpringbootTest, Rest-Assured, JUnit and Cucumber dependencies to the project
Create a source folder src/test/resources and create a feature file under src/test/resources
Create the Step Definition class or Glue Code for the Test Scenario under src/test/java directory
Create a Cucumber Runner class under src/test/java directory
Run the tests from Cucumber Test Runner
Run the tests from Command Line
Run the tests from TestNG
Generation of TestNG Reports
Cucumber Report Generation
Below is the structure of a SpringBoot application project
We need below files to create a SpringBoot Application.
SpringBootRestServiceApplication.java
The Spring Boot Application class generated with Spring Initializer. This class acts as the launching point for application.
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringBootRestServiceApplication { public static void main(String[] args) { SpringApplication.run(SpringBootRestServiceApplication.class, args); } }
Student.java
This is JPA Entity for Student class
import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; @Entity public class Student { @Id @GeneratedValue private Long id; @NotNull @Size(min = 4, message = "Name should have atleast 4 characters") private String name; @NotBlank(message = "passportNumber is mandatory") private String passportNumber; public Student() { super(); } public Student(Long id, String name, String passportNumber) { super(); this.id = id; this.name = name; this.passportNumber = passportNumber; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassportNumber() { return passportNumber; } public void setPassportNumber(String passportNumber) { this.passportNumber = passportNumber; } }
StudentRepository.java
This is JPA Repository for Student. This is created using Spring Data JpaRepository.
Spring Boot automatically loads the application.properties whenever it starts up. You can de reference values from the property file in the java code through the environment.
spring.jpa.defer-datasource-initialization=true
data.sql
Data is loaded from data.sql into Student table. Spring Boot would execute this script after the tables are created from the entities.
insert into student values(10001,'Annie', 'E1234567'); insert into student values(20001,'John', 'A1234568'); insert into student values(30001,'David','C1232268'); insert into student values(40001,'Amy','D213458');
Test Automation Framework Implementation
Step 1 – Add SpringbootTest, Cucumber, Rest-Assured and TestNG dependencies to the project (Maven project)
Step 2 – Create a source folder src/test/resources and create a feature file under src/test/resources
By default, the Maven project has src/test/java directory only. Create a new Source Folder under src/test with the name of resources. Create a folder name as Features within src/test/resources directory.
Create a feature file to test the Springboot application. Below is a sample feature file.
Feature: Verify springboot application using Cucumber and TestNG @ReceiveUserDetails Scenario Outline: Send a valid Request to get user details Given I send a request to the URL "/students" to get user details Then The response will return status 200 And The response contains id <studentID> and names "<studentNames>" and passport_no "<studentPassportNo>" Examples: |studentID |studentNames |studentPassportNo| |10001 |Annie |E1234567 | |20001 |John |A1234568 | |30001 |David |C1232268 | |40001 |Amy |D213458 | @CreateUser Scenario: Send a valid Request to create a user Given I send a request to the URL "/students" to create a user with name "Annie" and passportNo "E1234567" Then The response will return status 201 And Resend the request to the URL "/students" and the response returned contains name "Annie" and passport_no "E1234567"
Step 3 – Create the Step Definition class or Glue Code for the Test Scenariounder src/test/java
The corresponding step definition file of the above feature file is shown below.
import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasItem; import org.json.JSONObject; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.web.server.LocalServerPort; import io.cucumber.java.en.Given; import io.cucumber.java.en.Then; import io.cucumber.spring.CucumberContextConfiguration; import io.restassured.RestAssured; import io.restassured.http.ContentType; import io.restassured.response.ValidatableResponse; import io.restassured.specification.RequestSpecification; @CucumberContextConfiguration @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class SpringbootDefinitions { private final static String BASE_URI = "http://localhost"; @LocalServerPort private int port; private ValidatableResponse validatableResponse, validatableResponse1; private void configureRestAssured() { RestAssured.baseURI = BASE_URI; RestAssured.port = port; } protected RequestSpecification requestSpecification() { configureRestAssured(); return given(); } @Given("I send a request to the URL {string} to get user details") public void getStudentDetails(String endpoint) throws Throwable { validatableResponse = requestSpecification().contentType(ContentType.JSON).when().get(endpoint).then(); System.out.println("RESPONSE :" + validatableResponse.extract().asString()); } @Given("I send a request to the URL {string} to create a user with name {string} and passportNo {string}") public void createStudent(String endpoint, String studentName, String studentPassportNumber) throws Throwable { JSONObject student = new JSONObject(); student.put("name", studentName); student.put("passportNumber", studentPassportNumber); validatableResponse = requestSpecification().contentType(ContentType.JSON).body(student.toString()).when() .post(endpoint).then(); System.out.println("RESPONSE :" + validatableResponse.extract().asString()); } @Then("The response will return status {int}") public void verifyStatusCodeResponse(int status) { validatableResponse.assertThat().statusCode(equalTo(status)); } @Then("The response contains id {int} and names {string} and passport_no {string}") public void verifyResponse(int id, String studentName, String passportNo) { validatableResponse.assertThat().body("id", hasItem(id)).body(containsString(studentName)) .body(containsString(passportNo)); } @Then("Resend the request to the URL {string} and the response returned contains name {string} and passport_no {string}") public void verifyNewStudent(String endpoint, String studentName, String passportNo) { validatableResponse1 = requestSpecification().contentType(ContentType.JSON).when().get(endpoint).then(); System.out.println("RESPONSE :" + validatableResponse1.extract().asString()); validatableResponse1.assertThat().body(containsString(studentName)).body(containsString(passportNo)); } }
To make Cucumber aware of your test configuration you can annotate a configuration class on your glue path with @CucumberContextConfiguration and with one of the following annotations: @ContextConfiguration, @ContextHierarchy or @BootstrapWith.It is imported from:
By default, @SpringBootTest does not start the webEnvironment to refine further how your tests run. It has several options: MOCK(default), RANDOM_PORT, DEFINED_PORT, NONE.
RANDOM_PORT loads a WebServerApplicationContext and provides a real web environment. The embedded server is started and listens on a random port. LocalServerPort is imported from package:
Step 4 – Create a Cucumber TestNG Runner classunder src/test/java
A runner will help us to run the feature file and acts as an interlink between the feature file and StepDefinition Class. The TestRunner should be created within the directory src/test/java.
import io.cucumber.testng.AbstractTestNGCucumberTests; import io.cucumber.testng.CucumberOptions; @CucumberOptions(features = {"src/test/resources/Features"}, glue = {"com.example.demo.definitions"}) public class CucumberRunnerTests extends AbstractTestNGCucumberTests { }
The @CucumberOptions annotation is responsible for pointing to the right feature package, configuring the plugin for a better reporting of tests in the console output, and specifying the package where extra glue classes may be found. We use it to load configuration and classes that are shared between tests.
Step 5 – Run the tests from Cucumber Test Runner
You can execute the test script by right-clicking on TestRunner class -> Run As TestNG in Eclipse.
In case you are using IntelliJ, select "Run CucumberRunnerTests".
SpringBootTest creates an application context containing all the objects we need for the Integration Testing It, starts the embedded server, creates a web environment, and then enables methods to do Integration testing.
Step 6 – Run the tests from Command Line
Use the below command to run the tests through command line.
mvn clean test
Step 7 – Run the tests from TestNG
Create a testng.xml in the project as shown below:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"> <suite name = "Suite1"> <test name = "SpringBoot Cucumber TestNG Demo"> <classes> <class name = "com.example.demo.runner.CucumberRunnerTests"/> </classes> </test> </suite>
Step 8 - Generation of TestNG Reports
TestNG generates the various types of reports under test-output folder like emailable-report.html, index.html, testng-results.xml.
We are interested in the'emailable-report.html' report. Open "emailable-report.html", as this is an HTML report, and open it with the browser. The below image shows emailable-report.html.
TestNG also produce "index.html" report, and it resides under test-output folder. The below image shows index.html report.
Step 9 - Cucumber Report Generation
Add cucumber.properties under src/test/resources and add the below instruction in the file.
cucumber.publish.enabled=true
The link to the Cucumber Report is present in the execution status.
Below is the image of the Cucumber Report generated using Cucumber Service.
Complete Source Code: Refer to GitHub for source code.
Congratulations!! We are able to built a test framework to test SpringBoot application using Cucumber, Rest Assured and TestNG.
No comments:
Post a Comment