java config_error ai_generated true

org.junit.jupiter.api.extension.LifecycleMethodExecutionException: @BeforeAll method 'void setUp()' must be static unless the test class is annotated with @TestInstance(Lifecycle.PER_CLASS)

ID: java/junit-lifecycle-method-not-static

Also available as: JSON · Markdown · 中文
80%Fix Rate
82%Confidence
0Evidence
2025-02-14First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
8+ active

Root Cause

@BeforeAll or @AfterAll methods are not static in a per-method lifecycle test class.

generic

中文

在每次方法生命周期的测试类中,@BeforeAll 或 @AfterAll 方法不是静态的。

Workarounds

  1. 90% success Make method static
    @BeforeAll
    static void setUp() {
        // static setup
    }
  2. 85% success Use @TestInstance(Lifecycle.PER_CLASS)
    @TestInstance(Lifecycle.PER_CLASS)
    class MyTest {
        @BeforeAll
        void setUp() {
            // non-static setup
        }
    }

Dead Ends

Common approaches that don't work:

  1. Adding static keyword without changing method logic 60% fail

    Method may access instance fields which are not allowed.

  2. Removing @BeforeAll annotation 70% fail

    Setup logic won't run, causing test failures.