EasyMock 测试失败 java.lang.NoClassDefFoundError org.easymock.internal.ClassProxyFactory$2 on Java 17

EasyMock tests failing with java.lang.NoClassDefFoundError org.easymock.internal.ClassProxyFactory$2 on Java 17

自从升级到 Java17,我使用 EasyMock 的单元测试失败并出现以下异常

java.lang.NoClassDefFoundError: Could not initialize class org.easymock.internal.ClassProxyFactory at org.easymock.internal.ClassProxyFactory.createEnhancer(ClassProxyFactory.java:233) at org.easymock.internal.ClassProxyFactory.createProxy(ClassProxyFactory.java:165) at org.easymock.internal.MocksControl.createMock(MocksControl.java:107) at org.easymock.internal.MocksControl.createMock(MocksControl.java:80) at org.easymock.EasyMockSupport.mock(EasyMockSupport.java:77) at org.easymock.EasyMockSupport.createMock(EasyMockSupport.java:364)

我运行使用maven-surefire-plugin进行测试

Java 17 个包含 JEP 403 which strongly encapsulates JDK Internals.

由于 EasyMock 使用其中一些内部 API,因此需要通过设置命令行选项来访问这些 API add-opens

If you have to allow code on the class path to do deep reflection to access nonpublic members, then use the --add-opens runtime option.

https://docs.oracle.com/javase/9/migrate/toc.htm

正如this EasyMock issue

中所指出的

you need to open that package in command line

因此要修复异常 add-opens 需要将 maven-surefire-plugin

设置为 argLine
<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    ...
    <configuration>
        <argLine>
            --add-opens java.base/java.lang=ALL-UNNAMED
        </argLine>
    </configuration>
</plugin>