A setup, teardown or rule annotation of one JUnit version sits in a class whose tests run on the other, so it never runs

tx0/java:lifecycle-of-other-junitrecommendederrormaintainability/testsdepth fastbug, test

What it finds

JUnit 4 and JUnit 5 run different annotations. A class whose tests use JUnit 5's @Test runs on the JUnit 5 engine, which ignores JUnit 4's @Before, @After, @BeforeClass, @AfterClass and @Rule; a class whose tests use JUnit 4's @Test runs on JUnit 4, which ignores @BeforeEach, @AfterEach, @BeforeAll and @AfterAll. The setup then never runs, so tests work on state nobody prepared or leave behind what nobody cleans up, and often still pass.

Reported is a setup, teardown or rule annotation of one JUnit version on a member of a class whose own tests all use the other version. Rules are left alone in a class with JUnit 5's rule migration support or with an extension of its own, which may run them.

From the rule's source, lifecycle_of_other_junit.rs.

Example from the rule's tests
import org.junit.Before;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test; class NewStyleTest {  @Beforetx0 reports: @Before is from JUnit 4, but the tests of this class run on JUnit 5  public void setUp() {}   @BeforeEach  void prepare() {}   @Test  void works() {}}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/java:lifecycle-of-other-junit" = "error"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/java:lifecycle-of-other-junit] reason="..."

tx0 explain tx0/java:lifecycle-of-other-junit shows the same in the terminal. Found a false positive? Report it.