JUnit Assertions in Cucumber Framework

The Cucumber framework does not provide an assertion library. Instead, we integrate popular Java testing libraries like JUnit or TestNG to perform assertions.

In automation testing, assertions help verify whether the actual result matches the expected result. If an assertion fails, the test script immediately stops and the test is marked as failed.

In this tutorial, we'll focus on JUnit assertions, which are widely used with Cucumber in Java projects.

Unit Assertion Library

JUnit provides assertion methods in the org.junit.Assert class, which extends java.lang.Object

Types of JUnit Assertions

1. Boolean Assertions

assertTrue(condition) – Passes if condition is true.
assertFalse(condition) – Passes if condition is false.

Example:

package stepDefinitions;
import io.cucumber.java.en.Given;
import static org.junit.Assert.assertFalse;
public class MyStepdefs {
    @Given("user navigates to the URL in the browser")
    public void userNavigatesToTheUrlInTheBrowser() {
        System.out.println("User navigates to the URL...");
        int a = 1;
        boolean flag = a < 2; // true
        assertFalse(flag); // This will fail
    }
}


Output: 

java.lang.AssertionError
    at org.junit.Assert.assertFalse(Assert.java:75)

 

2. Null Assertions

assertNull(object) – Passes if object is null.
assertNotNull(object) – Passes if object is not null.

package stepDefinitions;
import io.cucumber.java.en.Given;
import static org.junit.Assert.assertNotNull;
public class MyStepdefs {
    @Given("user navigates to the URL in the browser")
    public void userNavigatesToTheUrlInTheBrowser() {
        String newStr = null;
        assertNotNull("The object newStr is null", newStr); // This will fail
    }
}


Output: 

java.lang.AssertionError: The object newStr is null


3. Equality Assertions

assertEquals(expected, actual)
assertNotEquals(expected, actual)

Supports delta for comparing floating point or int values with tolerance.

Example with delta:
package stepDefinitions;
import io.cucumber.java.en.Given;
import static org.junit.Assert.assertEquals;
public class MyStepdefs {
    @Given("user navigates to the URL in the browser")
    public void userNavigatesToTheUrlInTheBrowser() {
        int a = 1;
        int b = 3;
        int delta = 2;
        assertEquals("Check if values are equal within delta", a, b, delta); // Passes
    }
}


4. Array Assertions

assertArrayEquals(expectedArray, actualArray)

Syntax:

assertArrayEquals("Arrays didn't match", expectedArray, actualArray);


5. Fail Message

To intentionally fail a test (e.g., to mark a work-in-progress scenario):

import static org.junit.Assert.fail;
fail("Failing the test case intentionally");


6. Identity Assertions

assertSame(expected, actual) – Passes if both references point to the same object.

assertNotSame(expected, actual) – Passes if references point to different objects.
 

To learn more about TestNG assertions, check out this tutorial: TestNG Assertion

Related Tutorials