LWJGL/lwjgl3

Attempt to mock GL11 results in "Class redefinition failed, invalid class"

ixuz opened this issue · 3 comments

ixuz commented

Question

Any tips on how to properly mock classes of LWJGL?
For example when I try to static mock the GL11 class, it results in a class redefinition failed, invalid class error.

Reproduction of issue (Available here on GitHub):

  1. Create a new maven project (Yes on all prompts):
    $ mvn archetype:generate -DgroupId=com.example -DartifactId=gl11test
  2. Set compile source and target to 21 in pom.xml:
    <maven.compiler.source>21</maven.compiler.source>
    <maven.compiler.target>21</maven.compiler.target>
  1. Add LWJGL and Mockito dependency to pom.xml:
    <dependency>
      <groupId>org.lwjgl</groupId>
      <artifactId>lwjgl-opengl</artifactId>
      <version>3.3.3</version>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-core</artifactId>
      <version>5.11.0</version>
      <scope>test</scope>
    </dependency>
  1. Create src/test/java/com/example/JWJGLTest.java:
package com.example;
import org.lwjgl.opengl.GL11;
import static org.mockito.Mockito.mockStatic;
public class JWJGLTest {
    @Test public void tryMockStaticGL11() { mockStatic(GL11.class); }
}
  1. $ mvn clean test

Error:

[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.65 s <<< FAILURE! - in com.example.JWJGLTest
[ERROR] tryMockStaticGL11(com.example.JWJGLTest)  Time elapsed: 1.65 s  <<< ERROR!
java.lang.InternalError: class redefinition failed: invalid class
        at com.example.JWJGLTest.tryMockStaticGL11(JWJGLTest.java:6)

Any idea what could be wrong?

$ mvn -v
Apache Maven 3.6.3
Maven home: /usr/share/maven
Java version: 21.0.2, vendor: Eclipse Adoptium, runtime: /usr/lib/jvm/temurin-21-jdk-amd64
Default locale: en, platform encoding: UTF-8
OS name: "linux", version: "5.15.146.1-microsoft-standard-wsl2", arch: "amd64", family: "unix"

Hi @ixuz, I'm afraid this is a limitation of Mockito. Have a look at mockito/mockito#3273 for further information.

ixuz commented

Thanks so so much for the quick reply 👍

Yes, seems to be related to the GL11 classhaving native methods.
Tbh, I don't know exactly what that means, but thank you for the reference to the relevant issue.

On a final note, do you have any recommendation on how to unit test these kind of methods?

Meanwhile, I'll close this issue, as it seems like problem is more related to Mockito than LWJGL.

On a final note, do you have any recommendation on how to unit test these kind of methods?

Unfortunately, no. I would just test rendering by comparing screenshots and gbuffers.