JUnit
Assert Classes
Software Testing
Java Testing
Unit Testing

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 Stream match a specified list of strings.

Key Differences

  1. Package and Module: Assert belongs to JUnit 4, hence it's located in org.junit, while Assertions is under JUnit 5, bundled in org.junit.jupiter.api.
  2. Methodologies: Although both classes offer assertion capabilities, Assertions in JUnit 5 supports more versatile and powerful assertions suitable for modern testing paradigms, including lambda expressions and streaming APIs.
  3. Integration with JUnit 5 Features: Assertions integrates tightly with other JUnit 5 features like dynamic tests, test interfaces, and nested tests.
  4. Design Intent: While Assert is straightforward, Assertions is part of JUnit 5’s aim at making tests more powerful and expressive. This is evident from methods like assertAll and assertThrows, which make test intent clearer and error diagnostics more informative.

Usage Examples

JUnit 4 Assert Example:

java
1import org.junit.Test;
2import static org.junit.Assert.*;
3
4public class SampleTest {
5    @Test
6    public void testAssert() {
7        assertEquals("strings should be equal", "text", "text");
8        assertTrue("should be true", 1 > 0);
9    }
10}

JUnit 5 Assertions Example:

java
1import org.junit.jupiter.api.Test;
2import static org.junit.jupiter.api.Assertions.*;
3
4public class SampleTest {
5    @Test
6    public void testAssertions() {
7        assertAll("demo",
8            () -> assertEquals("text", "text"),
9            () -> assertTrue(1 > 0)
10        );
11    }
12}

Summary Table

FeatureAssert (JUnit 4)Assertions (JUnit 5)
Packageorg.junitorg.junit.jupiter.api
Exception HandlingNone specificImproved with methods like assertThrows
Grouped AssertionsNot directly supportedSupported with assertAll
Modern Java FeaturesLimitedSupports lambdas, streaming APIs, etc
IntegrationBasic JUnit featuresIntegrated 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.


Course illustration
Course illustration

All Rights Reserved.