Search code examples
easymockjava-17

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


Since upgradting to Java 17 my unit test that are using EasyMock are failing with to the following exception

java.lang.NoClassDefFoundError: Could not initialize class org.easymock.internal.ClassProxyFactory$2 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)

I am running the tests using maven-surefire-plugin.


Solution

  • Update: as of EasyMock 5.0.0 add-opens is not required anymore.

    Issues #274 and #300 have been resolved with version 5.0.0.

    So upgrading to EasyMock version 5 solves the issue.


    Java 17 contains JEP 403 which strongly encapsulates JDK Internals.

    As EasyMock uses some of these internal APIs access to these need to be made accessable by setting the command line option 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

    As pointed out in this EasyMock issue

    you need to open that package in command line

    So to fix the exception add-opens needs to be set as argLine for the maven-surefire-plugin

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