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
80%Fix Rate
82%Confidence
0Evidence
2025-02-14First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 8+ | active | — | — | — |
Root Cause
@BeforeAll or @AfterAll methods are not static in a per-method lifecycle test class.
generic中文
在每次方法生命周期的测试类中,@BeforeAll 或 @AfterAll 方法不是静态的。
Workarounds
-
90% success Make method static
@BeforeAll static void setUp() { // static setup } -
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:
-
Adding static keyword without changing method logic
60% fail
Method may access instance fields which are not allowed.
-
Removing @BeforeAll annotation
70% fail
Setup logic won't run, causing test failures.