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:
Output:
2. Null Assertions
assertNull(object) – Passes if object is null.
assertNotNull(object) – Passes if object is not null.
Output:
3. Equality Assertions
assertEquals(expected, actual)
assertNotEquals(expected, actual)
Supports delta for comparing floating point or int values with tolerance.
4. Array Assertions
assertArrayEquals(expectedArray, actualArray)
Syntax:
5. Fail Message
To intentionally fail a test (e.g., to mark a work-in-progress scenario):
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