You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring WebFlux集成JUnit测试遇Netty反射访问禁用错误(JDK13)

Fixing Netty Reflective Access Error in JDK 13 with Spring Boot 2.2.2

Hey there, I see you're hitting a frustrating reflective access error with Netty when running your JUnit test on JDK 13.0.1 alongside Spring Boot 2.2.2.RELEASE. Let’s break down what’s going on and how to fix it.

Why This Error Happens

There are two key reasons behind this issue:

  1. JDK Module System Restrictions: Starting with JDK 9, the Java Platform Module System (JPMS) locks down access to internal APIs like jdk.internal.misc.Unsafe. The error shows Netty’s PlatformDependent0 class is trying to access this internal class, but the java.base module won’t export it to the unnamed module your application runs in.
  2. Version Misalignment: Your pom.xml specifies <java.version>11</java.version>, but you’re running the test on JDK 13. Spring Boot 2.2.2 was built primarily for JDK 8 and 11, so it doesn’t account for JDK 13’s stricter reflection rules.

Solutions to Try

Since your project is configured for JDK 11, the simplest and most stable fix is switching your IntelliJ runtime to JDK 11:

  • Open IntelliJ’s Project Structure (File > Project Structure)
  • Go to Project Settings > Project and set "Project SDK" to JDK 11
  • Navigate to Project Settings > Modules and make sure "Module SDK" matches JDK 11
  • Update your JUnit run configuration to use JDK 11 as the runtime

This eliminates compatibility gaps between Spring Boot 2.2.2 and JDK 13 entirely.

2. Add JVM Flags to Allow Reflective Access

If you need to stick with JDK 13, grant Netty access to restricted internal APIs by adding these VM arguments to your JUnit run configuration:

--add-opens java.base/jdk.internal.misc=ALL-UNNAMED
--add-opens java.base/sun.nio.ch=ALL-UNNAMED
--add-opens java.base/java.nio=ALL-UNNAMED
--add-opens java.base/java.lang=ALL-UNNAMED

To set this in IntelliJ:

  • Open your test’s run configuration
  • Go to the Configuration tab
  • Paste the flags into the "VM options" field

If you run tests via Maven, add these flags to the maven-surefire-plugin in your pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <argLine>
                    --add-opens java.base/jdk.internal.misc=ALL-UNNAMED
                    --add-opens java.base/sun.nio.ch=ALL-UNNAMED
                    --add-opens java.base/java.nio=ALL-UNNAMED
                    --add-opens java.base/java.lang=ALL-UNNAMED
                </argLine>
            </configuration>
        </plugin>
    </plugins>
</build>

3. Upgrade Spring Boot (Long-Term Fix)

Spring Boot 2.3.0.RELEASE and newer officially support JDK 13. Upgrading will pull in a Netty version compatible with JDK 13’s module system:

  • Update your parent pom’s version to a stable 2.3.x release (like 2.3.12.RELEASE) or newer:
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.12.RELEASE</version>
</parent>
  • You can also update <java.version> to 13 if you want to officially target JDK 13.

Verification

After applying any of these fixes, re-run your JUnit test. The Netty reflective access error should be resolved, and your test should execute as expected.

内容的提问来源于stack exchange,提问作者robyp7

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:10:20