Skip to main content

Redundant assertions

Sometimes overlapping assertions are used in sequence, where one would imply the other. For example, checking that a list is not null before checking its size or contents is redundant, as a null list would cause the subsequent assertions to fail anyway.

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

import java.util.List;

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

class RedundantListTest {

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

@Test
void assertListSize() {
assertNotNull(list);
assertEquals(3, list.size());
assertTrue(list.contains("b"));
}
}
warning

The assertNotNull(list) is redundant because if list were null, the subsequent assertions would throw a NullPointerException anyway.