Skip to main content

Backwards

Between JUnit 4 and JUnit 5 the order of arguments in assertions was swapped to improve readability. In JUnit 4 the expected value came second, while in JUnit 5 it comes first. This change was made to align with the natural reading order of "actual value should be expected value". There is a danger though for folks who upgraded without changing their existing tests, that the arguments are now in the wrong order.

Confusing failure messages

This can lead to confusing error messages when tests fail, as the expected and actual values will be reported incorrectly.

ArgumentOrderTest.java
import org.junit.jupiter.api.Test;

import java.util.List;

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

class ArgumentOrderTest {

List<String> list = List.of("a", "b", "c");

@Test
void confusingFailureMessage() {
assertEquals(list.size(), 4);
}
}
warning

Warning: The arguments to assertEquals are in the wrong order. The actual value should be the first argument, and the expected value should be the second argument.

Incorrectly passing tests

Worse still, if the values are of the same type, tests may pass when they should fail, or vice versa.

ArgumentOrderTest.java
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertNotNull;

class ArgumentOrderTest {
@Test
void incorrectlyPassing() {
String actual = null;
assertNotNull("message", actual);
}
}
danger

The arguments to assertNotNull are in the wrong order. This test will incorrectly pass because the string "message" is not null.