Jupiter assertions

Let's move on to the JUnit 5 programming model. Jupiter comes with many of the assertion methods such as the ones in JUnit 4, and also adds several that can be used with Java 8 lambdas. All JUnit Jupiter assertions are static methods in the Assertions class located in org.junit.jupiter package.

The following picture shows the complete list of these methods:

Complete list of Jupiter assertions (class org.junit.jupiter.Assertions)

The following table reviews the different types of basic assertions in Jupiter:

Assertion

Description

fail

Fails a test with a given message and/or exception

assertTrue

Asserts that a supplied condition is true

assertFalse

Asserts that a supplied condition is false

assertNull

Asserts that a supplied object is null

assertNotNull

Asserts that a supplied object is not null

assertEquals

Asserts that two supplied objects are equal

assertArrayEquals

Asserts that two supplied arrays are equal

assertIterableEquals

Asserts that two iterable objects are deeply equal

assertLinesMatch

Asserts that two lists of Strings are equals

assertNotEquals

Asserts that two supplied objects are not equal

assertSame

Asserts that two objects are the same, compared with ==

assertNotSame

Asserts that two objects are different, compared with !=

For each of the assertions contained in the table, an optional failure message (String) can be provided. This message is always the last parameter in the assertion method. This is a small difference with respect to JUnit 4, in which this message was the first parameter in the method invocation.

The following example shows a test using the  assertEquals, assertTrue, and assertFalse assertion. Note that we are importing the static assertion methods at the beginning of the class in order to improve the readability of the test logic. In the example, we find the assertEquals method, in this case comparing two primitive types (it could also be used for objects). Second, the method assertTrue evaluates if a boolean expression is true. Third, the method assertFalse evaluates if a Boolean expression is false. In this case, notice that the message is created as a Lamdba expression. This way, assertion messages are lazily evaluated to avoid constructing complex messages unnecessarily:

package io.github.bonigarcia;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

class StandardAssertionsTest {

@Test
void standardAssertions() {
assertEquals(2, 2);
assertTrue(true,
"The optional assertion message is now the last parameter");
assertFalse(false, () -> "Really " + "expensive " + "message"
+ ".");
}

}

The following parts of this section review the advance assertions provided by Jupiter: assertAll, assertThrows, assertTimeout, and assertTimeoutPreemptively.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.15.43