JUnit Mixed Versions

ID

java.junit_mixed_versions

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

best-practice, junit, testing

Description

Reports classes that mix JUnit 4 (org.junit.) and JUnit 5 (org.junit.jupiter.) annotations. Only one test framework runner is active per class, so the annotations from the other framework are silently ignored.

Rationale

Reliability — Setup, teardown, or test methods annotated with the inactive framework version will not execute, leading to incomplete test coverage.

Remediation

Non-compliant code

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

class MyTest {
    @Before public void setUp() { }  // JUnit 4
    @Test void testFoo() { }          // JUnit 5
}

Compliant code

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

class MyTest {
    @BeforeEach void setUp() { }
    @Test void testFoo() { }
}