Integration of Rest Assured with JUnit 5

  

Integrating RestAssured with JUnit 5 for API testing is a popular combination for writing clean, structured, and automated API tests in Java. Here's a detailed explanation and sample code:


What is RestAssured?

RestAssured is a Java-based library used to test RESTful APIs. It provides a domain-specific language (DSL) for making requests and validating responses.


What is JUnit 5?

JUnit 5 is the latest version of the popular unit testing framework for Java. It provides annotations, assertions, and other tools to write test cases.


Steps to Integrate RestAssured with JUnit 5

1. Add Maven Dependencies


<dependencies>
    <!-- RestAssured for API testing -->
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>5.4.0</version>
        <scope>test</scope>
    </dependency>

    <!-- JUnit 5 -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.10.0</version>
        <scope>test</scope>
    </dependency>

    <!-- JUnit 5 Engine -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.10.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>




Sample Test Code


package api;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static org.hamcrest.Matchers.*;
import static io.restassured.RestAssured.*;

public class SampleApiTest {

    @BeforeAll
    public static void setup() {
        // Set the base URI for all tests
        RestAssured.baseURI = "https://jsonplaceholder.typicode.com";
    }

    @Test
    public void testGetPosts() {
        given()
            .when()
            .get("/posts")
            .then()
            .statusCode(200)
            .body("size()", greaterThan(0));
    }

    @Test
    public void testGetPostById() {
        given()
            .when()
            .get("/posts/1")
            .then()
            .statusCode(200)
            .body("id", equalTo(1))
            .body("userId", notNullValue());
    }

    @Test
    public void testCreatePost() {
        String jsonBody = """
            {
                "title": "foo",
                "body": "bar",
                "userId": 1
            }
        """;

        given()
            .contentType(ContentType.JSON)
            .body(jsonBody)
            .when()
            .post("/posts")
            .then()
            .statusCode(201)
            .body("title", equalTo("foo"))
            .body("userId", equalTo(1));
    }
}




Explanation of Annotations

AnnotationDescription
@BeforeAllRuns once before all test methods in the class.
@TestMarks a method as a test case.


Assertions and Features Used

  • statusCode(200): Asserts the HTTP status.

  • body("id", equalTo(1)): Asserts the value in response body.

  • greaterThan(0): Asserts list size.

  • notNullValue(): Ensures the value is present.


How to Run

  • In Eclipse/IntelliJ:  Right-click the test class → Run as JUnit Test.
  • Using Maven:

mvn test

Integration of Rest Assured with JUnit 4

  

To integrate RestAssured with JUnit 4 for API testing, you can follow these steps:


Overview

  • RestAssured: A Java DSL for testing REST services.

  • JUnit 4: A popular unit testing framework for Java.

Integration Workflow

  • Add RestAssured and JUnit 4 dependencies.
  • Write test cases using JUnit 4 annotations (@Test@Before, etc.).
  • Use RestAssured to make HTTP requests and validate responses.


1. Maven Dependencies


<dependencies>
    <!-- RestAssured -->
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>5.4.0</version>
        <scope>test</scope>
    </dependency>

    <!-- JUnit 4 -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
    </dependency>

    <!-- JSON path (optional but useful) -->
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>json-path</artifactId>
        <version>5.4.0</version>
    </dependency>
</dependencies>




2. Simple GET API Test Example


import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.junit.Before;
import org.junit.Test;

import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;

public class APITest {

    @Before
    public void setup() {
        RestAssured.baseURI = "https://jsonplaceholder.typicode.com";
    }

    @Test
    public void testGetUser() {
        given().
            log().all().
        when().
            get("/users/1").
        then().
            log().all().
            assertThat().
            statusCode(200).
            body("id", equalTo(1)).
            body("username", notNullValue());
    }

    @Test
    public void testGetInvalidUser() {
        given().
            log().all().
        when().
            get("/users/9999").
        then().
            log().all().
            assertThat().
            statusCode(404);
    }
}




3. Explanation of Code


SectionDescription
@BeforeSets the base URI for all API requests.
given()Prepares the request (params, headers, etc.).
when()Specifies the request type (GET/POST/PUT/DELETE).
then()Asserts the response: status, body content, etc.
log().all()Logs request/response details to console (for debugging).


4. Run the Tests

You can run these tests using:

  • Eclipse/IntelliJ (right-click → Run as JUnit Test)

  • Maven CLI:


mvn test



5. Important Points:

  • Externalize base URLs using config files or environment variables.

  • Use @BeforeClass for expensive setups.

  • Use assertions to validate headers, response time, etc.

  • Use POJOs (Plain Old Java Objects) for complex JSON validation with RestAssured + Gson or Jackson.

Integration of Karate API with JUnit 5

  

Integrating JUnit 5 with Karate API Testing framework allows you to run your BDD-style .feature files as part of your unit/integration test suite using the JUnit 5 engine. Below is a detailed guide and working code example.


Step-by-Step Integration: JUnit 5 + Karate API


1. Add Dependencies in pom.xml



<dependencies>
    <!-- Karate core -->
    <dependency>
        <groupId>com.intuit.karate</groupId>
        <artifactId>karate-junit5</artifactId>
        <version>1.4.1</version> <!-- or latest -->
        <scope>test</scope>
    </dependency>

    <!-- Optional: For HTML Reporting -->
    <dependency>
        <groupId>net.masterthought</groupId>
        <artifactId>cucumber-reporting</artifactId>
        <version>5.7.0</version>
    </dependency>
</dependencies>




2. Create Karate Test Feature File

src/test/resources/examples/sample.feature


Feature: Sample Karate Test

  Scenario: Get Request Test
    Given url 'https://jsonplaceholder.typicode.com/posts/1'
    When method get
    Then status 200
    And match response.id == 1




3. Create JUnit 5 Test Runner

src/test/java/examples/RunKarateTest.java


package examples;

import com.intuit.karate.junit5.Karate;

class RunKarateTest {

    @Karate.Test
    Karate testAll() {
        // Run all feature files in the package `examples`
        return Karate.run().relativeTo(getClass());
    }

    @Karate.Test
    Karate testSpecificFeature() {
        // Run a specific feature file
        return Karate.run("sample").relativeTo(getClass());
    }
}



4. Run Tests

You can run the tests:

  • From IDE (IntelliJ / Eclipse) — right-click RunKarateTest → Run.

  • From Maven:


mvn test


5. View Reports

Karate generates HTML reports by default at:

target/karate-reports/karate-summary.html

Integration of Selenium, Cucumber, and JUnit

  

To integrate SeleniumCucumber, and JUnit, you can build a Behavior Driven Development (BDD) test framework that allows you to define test scenarios in Gherkin (plain English), implement those steps in Java (using Selenium for UI interactions), and execute them using JUnit.


Components Overview:


ComponentRole
Selenium WebDriverAutomates browser actions
CucumberProvides BDD support via .feature files
JUnitExecutes the test suite (test runner)




Below are the steps to integrate Selenium, Cucumber and JUnit


1. Maven Dependencies


<dependencies>
    <!-- Selenium -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.19.1</version>
    </dependency>

    <!-- Cucumber -->
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>7.14.0</version>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-junit</artifactId>
        <version>7.14.0</version>
        <scope>test</scope>
    </dependency>

    <!-- JUnit -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>




2. Feature File 


Feature: Google Search Functionality

  Scenario: Search for a keyword on Google
    Given I open the Chrome browser
    When I open "https://www.google.com"
    And I search for "Cucumber BDD"
    Then I should see the results page




3. Step Definition file


package stepDefinitions;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.cucumber.java.en.*;

import static org.junit.Assert.*;

public class GoogleSearchSteps {
    WebDriver driver;

    @Given("I open the Chrome browser")
    public void i_open_the_chrome_browser() {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver();
    }

    @When("I open {string}")
    public void i_open(String url) {
        driver.get(url);
    }

    @When("I search for {string}")
    public void i_search_for(String query) {
        WebElement searchBox = driver.findElement(By.name("q"));
        searchBox.sendKeys(query);
        searchBox.submit();
    }

    @Then("I should see the results page")
    public void i_should_see_the_results_page() {
        assertTrue(driver.getTitle().toLowerCase().contains("cucumber bdd"));
        driver.quit();
    }
}




4. JUnit Test Runner


package runners;

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

@RunWith(Cucumber.class)
@CucumberOptions(
    features = "src/test/resources/features",
    glue = {"stepDefinitions"},
    plugin = { "pretty", "html:target/cucumber-reports" },
    monochrome = true
)
public class TestRunner {
}


How to Run the Tests

Right-click on TestRunner.java > Run As > JUnit Test
or
Use the Maven command:

mvn test


 Output

  • Browser opens Google

  • Types the query and submits

  • Assertion is done on the title

  • Generates HTML report in: target/cucumber-reports/index.html

What is TDD and how to use JUnit as TDD

  

What is TDD (Test-Driven Development)?

Test-Driven Development (TDD) is a software development methodology where you write tests before writing actual code. The main goal of TDD is to improve the design, quality, and maintainability of code.


TDD Workflow (The Red-Green-Refactor Cycle)

  1. Red – Write a failing test

    • Write a test case for the desired functionality before writing the code.

    • Since the code doesn’t exist yet, the test will fail.

  2. Green – Make the test pass

    • Write the minimal amount of code needed to make the test pass.

    • Focus only on passing the test.

  3. Refactor – Improve the code

    • Clean up the code while ensuring that all tests still pass.

    • Improve readability, remove duplication, optimize logic.

This cycle continues for each new piece of functionality.


Benefits of TDD

  • Forces requirement clarity before implementation.

  • Results in better design and decoupled components.

  • Reduces bugs due to automated tests.

  • Makes refactoring safe (you have tests as safety net).

  • Improves developer confidence and code coverage.


How to Use JUnit for TDD in Java

JUnit is a widely used testing framework in Java, perfect for TDD.


TDD Example with JUnit

Let’s develop a method to check if a number is prime using TDD.


Step 1: Write Failing Test


import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

public class PrimeUtilTest {

    @Test
    public void testIsPrime() {
        assertTrue(PrimeUtil.isPrime(5));  // test will fail: isPrime not implemented
        assertFalse(PrimeUtil.isPrime(4));
        assertTrue(PrimeUtil.isPrime(2));
    }
}




Step 2: Write Minimal Code to Pass


public class PrimeUtil {

    public static boolean isPrime(int number) {
        if (number <= 1) return false;
        for (int i = 2; i <= Math.sqrt(number); i++) {
            if (number % i == 0) return false;
        }
        return true;
    }
}



Step 3: Refactor if Needed

  • You can extract logic, rename variables, or make code cleaner.

  • No need to change behavior; just improve structure.



Best Practices When Using JUnit for TDD

  • Use descriptive names for tests (testAddTwoNumbersReturnsSum)

  • Start simple; avoid over-engineering.

  • Test both positive and negative cases.

  • Use tools like Maven or Gradle for test automation.

  • Keep test methods independent and isolated.




At a glance:


StepActionStatus
RedWrite test that failsDone
GreenWrite code to make the test passDone
RefactorClean up code without breaking the testOptional

Parameterization in JUnit

  

What is Parameterization in JUnit?

Parameterization in JUnit allows you to run the same test multiple times with different inputs. This is useful when you want to test a method using a variety of inputs and expected outputs without duplicating test code.

JUnit provides two main ways to do parameterized testing:

  • In JUnit 4: using @RunWith(Parameterized.class)

  • In JUnit 5: using the @ParameterizedTest annotation from org.junit.jupiter.params



JUnit 4 Parameterized Test

Steps:

  • Annotate the class with @RunWith(Parameterized.class)
  • Create a method annotated with @Parameters to return test data.
  • Define fields to hold parameters.
  • Create a constructor to initialize them.
  • Write your test method using those parameters.


Example:


import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
public class AdditionTest {

    private int a;
    private int b;
    private int expectedSum;

    // Constructor to initialize parameters
    public AdditionTest(int a, int b, int expectedSum) {
        this.a = a;
        this.b = b;
        this.expectedSum = expectedSum;
    }

    // This method provides test data
    @Parameterized.Parameters
    public static Collection<Object[]> testData() {
        return Arrays.asList(new Object[][] {
            {1, 2, 3},
            {5, 3, 8},
            {10, 15, 25},
            {0, 0, 0}
        });
    }

    @Test
    public void testAddition() {
        assertEquals(expectedSum, a + b);
    }
}




JUnit 5 Parameterized Test

Steps:

  • Add dependency for junit-jupiter-params
  • Use @ParameterizedTest annotation.
  • Use @ValueSource@CsvSource, or @MethodSource to provide values.



Example 1: Using @ValueSource


import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class OddNumberTest {

    @ParameterizedTest
    @ValueSource(ints = {1, 3, 5, 7, 9})
    public void testIsOdd(int number) {
        assertTrue(number % 2 != 0);
    }
}





Example 2: Using @CsvSource


import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class AdditionTest {

    @ParameterizedTest
    @CsvSource({
        "1, 2, 3",
        "5, 3, 8",
        "10, 15, 25"
    })
    void testAddition(int a, int b, int expectedSum) {
        assertEquals(expectedSum, a + b);
    }
}




Comparison:


FeatureJUnit 4JUnit 5
Setup@RunWith(Parameterized.class)@ParameterizedTest
Data source@Parameters (method)@ValueSource, @CsvSource, @MethodSource
Constructor requiredYesNo
External data flexibilityLessMore flexible