A JUnit setup, teardown or JUnit 4 test method has a modifier or return type that JUnit rejects, so the class fails to run
What it finds
JUnit checks the shape of setup and teardown methods and refuses a class whose methods do not fit, so its tests do not run. JUnit 5 requires lifecycle methods to return void, and @BeforeEach and @AfterEach methods not to be static. JUnit 4 requires @BeforeClass and @AfterClass methods to be public static void, and @Before, @After and @Test methods to be public void and not static.
Reported is a method whose JUnit lifecycle annotation, or JUnit 4 @Test, resolved through the imports, asks for a shape the method does not have. Whether JUnit 5's @BeforeAll must be static depends on a lifecycle that composed annotations and junit-platform.properties can set, so the rule does not judge it.
From the rule's source, lifecycle_method_shape.rs.
import org.junit.jupiter.api.AfterAll;import org.junit.jupiter.api.BeforeAll;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Test; class ServerTest { @BeforeAlltx0 reports: JUnit 5 requires this method to return void static boolean start() { return true; } @AfterAll static void stop() {} @BeforeEachtx0 reports: JUnit 5 requires this method not to be static static void reset() {} @BeforeEach private void clear() {} @BeforeAll void perClass() {} @Test void works() {}} 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:
[rules]
"tx0/java:lifecycle-method-shape" = "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-method-shape] reason="..."tx0 explain tx0/java:lifecycle-method-shape shows the same in the terminal. Found a false positive? Report it.