java config_error ai_generated true

测试实例化异常:测试类 [com.example.OuterTest$InnerTest] 必须是静态内部类

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

其他格式: JSON · Markdown 中文 · English
80%修复率
83%置信度
0证据数
2025-04-01首次发现

版本兼容性

版本状态引入弃用备注
8+ active

根因分析

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

English

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

generic

解决方案

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

无效尝试

常见但无效的做法:

  1. Removing @Nested annotation 60% 失败

    Loses organizational structure; tests may run but not nested.

  2. Making outer class non-static 50% 失败

    Outer class must be static for @Nested to work.