java config_error ai_generated true

org.junit.jupiter.api.extension.TestInstantiationException: Test class [com.example.OuterTest$InnerTest] must be a static inner class

ID: java/junit-nested-test-not-inner

Also available as: JSON · Markdown · 中文
80%Fix Rate
83%Confidence
0Evidence
2025-04-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
8+ active

Root Cause

@Nested test classes must be static inner classes, but they are non-static.

generic

中文

@Nested 测试类必须是静态内部类,但当前是非静态的。

Workarounds

  1. 95% success Add static modifier to inner class
    @Nested
    static class InnerTest {
        @Test
        void test() { }
    }
  2. 80% success Move test methods to outer class
    // Remove @Nested and put tests directly in outer class

Dead Ends

Common approaches that don't work:

  1. Removing @Nested annotation 60% fail

    Loses organizational structure; tests may run but not nested.

  2. Making outer class non-static 50% fail

    Outer class must be static for @Nested to work.