JUnit 5 Obsolete Assertions

ID

java.junit5_obsolete_assertions

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

best-practice, junit, testing

Description

Reports JUnit 5 test classes that import and use junit.framework.Assert or org.junit.Assert instead of org.junit.jupiter.api.Assertions. The legacy assertion classes lack JUnit 5 features such as lambda-based messages and grouped assertions.

Rationale

Reliability — Mixing assertion libraries creates inconsistency and prevents use of modern assertion features like assertThrows and assertAll.

Remediation

Non-compliant code

import org.junit.Assert;
import org.junit.jupiter.api.Test;

class MyTest {
    @Test void testFoo() {
        Assert.assertEquals("a", "a"); // legacy assertion
    }
}

Compliant code

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class MyTest {
    @Test void testFoo() {
        Assertions.assertEquals("a", "a");
    }
}