WrenSecurity / wrensec-commons

Community fork of ForgeRock Commons, which contains common utility code used by multiple products originally developed by ForgeRock.
http://wrensecurity.org
0 stars 10 forks source link

Mockito Upgrade Blocker: Mockito cannot mock `javax.naming.Context` in JDK 8+ #17

Closed Kortanul closed 5 years ago

Kortanul commented 5 years ago

Affected Versions

Build Environment

Steps to Reproduce

  1. Switch to Java 8u171 or later.
  2. Check out wrensec-commons on the feature/upgrade-mockito branch.
  3. Attempt to build CSV audit with mvn clean install -DignoreArtifactSigs.

Expected Results

Actual Results

Tests fail with the following error:

[ERROR] Tests run: 8, Failures: 1, Errors: 0, Skipped: 7, Time elapsed: 5.213 s <<< FAILURE! - in TestSuite
[ERROR] testContextLoading(org.forgerock.audit.handlers.jms.JndiJmsContextManagerTest)  Time elapsed: 2.472 s  <<< FAILURE!
org.mockito.exceptions.base.MockitoException:

Mockito cannot mock this class: interface javax.naming.Context.

Mockito can only mock non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.

Java               : 1.8
JVM vendor name    : Oracle Corporation
JVM vendor version : 25.181-b13
JVM name           : OpenJDK 64-Bit Server VM
JVM version        : 1.8.0_181-8u181-b13-1ubuntu0.18.04.1-b13
JVM info           : mixed mode
OS name            : Linux
OS version         : 4.15.0-39-generic

Underlying exception : java.lang.IllegalStateException: Error invoking java.lang.ClassLoader#defineClass
        at org.forgerock.audit.handlers.jms.JndiJmsContextManagerTest.testContextLoading(JndiJmsContextManagerTest.java:85)
Caused by: java.lang.IllegalStateException: Error invoking java.lang.ClassLoader#defineClass
        at org.forgerock.audit.handlers.jms.JndiJmsContextManagerTest.testContextLoading(JndiJmsContextManagerTest.java:85)
Caused by: java.lang.NoClassDefFoundError: javax/naming/Context
        at org.forgerock.audit.handlers.jms.JndiJmsContextManagerTest.testContextLoading(JndiJmsContextManagerTest.java:85)

[INFO]
[INFO] Results:
[INFO]
[ERROR] Failures:
[ERROR]
Mockito cannot mock this class: interface javax.naming.Context.

Mockito can only mock non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.

Java               : 1.8
JVM vendor name    : Oracle Corporation
JVM vendor version : 25.181-b13
JVM name           : OpenJDK 64-Bit Server VM
JVM version        : 1.8.0_181-8u181-b13-1ubuntu0.18.04.1-b13
JVM info           : mixed mode
OS name            : Linux
OS version         : 4.15.0-39-generic

Underlying exception : java.lang.IllegalStateException: Error invoking java.lang.ClassLoader#defineClass
[INFO]
[ERROR] Tests run: 8, Failures: 1, Errors: 0, Skipped: 7

Additional Notes

The class indicated javax.naming.context is part of the JDK and is always in the classpath. Therefore, this error must have something to do with class validation or other components of class loading that Bytebuddy (what Mockito uses for code generation) works with.