differences between 2 JUnit Assert classes
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
JUnit testing framework provides classes and methods to allow the writing of test cases and checking of diverse conditions during runtime. Among the classes provided by JUnit for asserting conditions in unit tests are Assert and Assertions. Even though they belong to different versions of JUnit (JUnit 4 vs. JUnit 5), they essentially perform similar tasks but vary in usage, features, and how they integrate with the JUnit ecosystem. Below, we explore these differences in greater detail.
Assert Class (JUnit 4)
The Assert class in JUnit 4 is part of org.junit package and offers a set of static methods to assert conditions in test code. These methods are used to ensure that the code under test behaves as expected. Common methods include:
- assertTrue: Checks that a condition is true.
- assertFalse: Checks that a condition is false.
- assertEquals: Checks that two values are equal, with an option for using a delta in case of comparing floating point numbers.
- assertNull: Checks that an object is null.
- assertNotNull: Checks that an object is not null.
- assertSame: Checks that two references point to the exact same object.
- assertNotSame: Checks that two references do not point to the same object.
Assertions Class (JUnit 5)
In JUnit 5, the Assertions class resides in org.junit.jupiter.api package and provides similar functionality to the Assert class but with enhanced capabilities and integration with the JUnit Jupiter programming and extension models. New methods introduced include:
- assertThrows: To assert that execution of a particular code block throws a specific exception.
- assertAll: Allows grouping of multiple assertions into a single unit.
- assertTimeout: Checks that a particular block of code completes before a certain timeout.
- assertIterableEquals: To verify that two iterables contain the same elements in the same order.
- assertLinesMatch: To check if the lines of a
Streammatch a specified list of strings.
Key Differences
- Package and Module:
Assertbelongs to JUnit 4, hence it's located inorg.junit, whileAssertionsis under JUnit 5, bundled inorg.junit.jupiter.api. - Methodologies: Although both classes offer assertion capabilities,
Assertionsin JUnit 5 supports more versatile and powerful assertions suitable for modern testing paradigms, including lambda expressions and streaming APIs. - Integration with JUnit 5 Features:
Assertionsintegrates tightly with other JUnit 5 features like dynamic tests, test interfaces, and nested tests. - Design Intent: While
Assertis straightforward,Assertionsis part of JUnit 5’s aim at making tests more powerful and expressive. This is evident from methods likeassertAllandassertThrows, which make test intent clearer and error diagnostics more informative.
Usage Examples
JUnit 4 Assert Example:
JUnit 5 Assertions Example:
Summary Table
| Feature | Assert (JUnit 4) | Assertions (JUnit 5) |
| Package | org.junit | org.junit.jupiter.api |
| Exception Handling | None specific | Improved with methods like assertThrows |
| Grouped Assertions | Not directly supported | Supported with assertAll |
| Modern Java Features | Limited | Supports lambdas, streaming APIs, etc |
| Integration | Basic JUnit features | Integrated with new JUnit Jupiter features |
In conclusion, while Assert and Assertions both serve the primary function of validating conditions during tests, Assertions offers a more modern, flexible, and powerful approach conducive to advanced testing practices and integration with other JUnit 5 features. Transitioning to Assertions can significantly benefit teams looking to leverage the full power of modern Java features in automated tests.

