添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
贪玩的火车  ·  org.mockito.Answers.ge ...·  3 周前    · 
玩命的企鹅  ·  Answer (Mockito 3.2.4 ...·  3 周前    · 
深情的钱包  ·  C# ...·  3 月前    · 
爱跑步的松鼠  ·  中国新闻网·  5 月前    · 
挂过科的机器猫  ·  MasterGo ...·  1 年前    · 
沉稳的香菜  ·  如何在 Ubuntu 22.04 LTS ...·  1 年前    · 

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I have the same problem on Java 10 (and not Java 8) with mocked class coming from Kotlin code.

Using the -Dnet.bytebuddy.experimental=true flag on maven build didn't change things.

The only solution I found was to add the byte buddy dependencies under a specific Java10 profile

<dependency>
                    <groupId>net.bytebuddy</groupId>
                    <artifactId>byte-buddy</artifactId>
                    <version>1.8.13</version>
                    <scope>compile</scope>
                </dependency>
                <dependency>
                    <groupId>net.bytebuddy</groupId>
                    <artifactId>byte-buddy-agent</artifactId>
                    <version>1.8.13</version>
                    <scope>compile</scope>
                </dependency>
                <dependency>
                    <groupId>org.objenesis</groupId>
                    <artifactId>objenesis</artifactId>
                    <version>2.6</version>
                    <scope>compile</scope>
                </dependency>

Using the latest version of Mockito (2.22.0), simply adding the following as a test dependency to my pom.xml allowed the tests to run happily:

 <dependency>
  <groupId>net.bytebuddy</groupId>
  <artifactId>byte-buddy</artifactId>
  <version>1.8.22</version>
  <scope>test</scope>
</dependency>

Still waiting for ASM to reach 7.0, then I am spinning up a release and patch Mockito upstream.

ASM 7.0 was released yesterday, @raphw

Trying to migrate a Gradle project from Java 8 to Java 11 (with the new module-info.java), I get the following error. My issue seems different from #1419, and having net.bytebuddy.experimental=true in gradle.properties has no effect. (FYI, I'm basically setting various module-related flags when compiling and running tests in the module path as described in the Gradle doc, since there is no working plugin properly supporting Jigsaw.)

com.google.cloud.tools.jib.event.EventHandlersTest > testAdd FAILED
    java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
        at [email protected]/org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:74)
        at com.sun.proxy.$Proxy14.isTypeMockable(Unknown Source)
        at [email protected]/org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:29)
        at [email protected]/org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)
        at [email protected]/org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:238)
        at [email protected]/org.mockito.internal.creation.MockSettingsImpl.build(MockSettingsImpl.java:226)
        at [email protected]/org.mockito.internal.MockitoCore.mock(MockitoCore.java:68)
        at [email protected]/org.mockito.Mockito.mock(Mockito.java:1896)
        at [email protected]/org.mockito.Mockito.mock(Mockito.java:1805)
        at com.google.cloud.tools.jib/com.google.cloud.tools.jib.event.EventHandlersTest.testAdd(EventHandlersTest.java:67)
        Caused by:
        java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in java.lang.CompoundEnumeration@49b0303e
            Caused by:
            org.mockito.exceptions.base.MockitoInitializationException:
            Could not initialize inline Byte Buddy mock maker. (This mock maker is not supported on Android.)
            Java               : 11
            JVM vendor name    : Oracle Corporation
            JVM vendor version : 11.0.1+13
            JVM name           : OpenJDK 64-Bit Server VM
            JVM version        : 11.0.1+13
            JVM info           : mixed mode
            OS name            : Linux
            OS version         : 4.18.10-1rodete2-amd64
                Caused by:
                java.lang.IllegalStateException:
                Mockito failed to inject the MockMethodDispatcher class into the bootstrap class loader
                It seems like your current VM does not support the instrumentation API correctly.
                    Caused by:
                    java.lang.ClassNotFoundException: org.mockito.internal.creation.bytebuddy.MockMethodDispatcher

Isn't org.mockito.internal.creation.bytebuddy.MockMethodDispatcher in the same org.mockito module that is trying to load the class inside it? Can't figure out why the module cannot load it.

I worked a lot with supporting the module system recently and fixed a lot of issues in yesterday night's release. Could you try with this release?

The MockMethodDispatcher class is injected into the bootstrap class loader to be universally visible. There is a chance that I am missing something there. Does everthing work for you if you are not using Mockito as a named module?

Thanks @raphw,

We are on the Java 8 world, and everything has been working fine. As I said, I am experimenting moving everything to Java 11.

I've compiled the repo locally, generated mockito-core-2.23.17.jar, and loaded it in my project. The log message shows that the module version is 2.23.17. Now it gives java.lang.ClassNotFoundException: net.bytebuddy.agent.ByteBuddyAgent, regardless of net.bytebuddy.experimental=true.

com.google.cloud.tools.jib.event.EventHandlersTest > testAdd FAILED
    java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
        at [email protected]/org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:74)
        at com.sun.proxy.$Proxy14.isTypeMockable(Unknown Source)
        at [email protected]/org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:29)
        at [email protected]/org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)
        at [email protected]/org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:238)
        at [email protected]/org.mockito.internal.creation.MockSettingsImpl.build(MockSettingsImpl.java:226)
        at [email protected]/org.mockito.internal.MockitoCore.mock(MockitoCore.java:61)
        at [email protected]/org.mockito.Mockito.mock(Mockito.java:1896)
        at [email protected]/org.mockito.Mockito.mock(Mockito.java:1805)
        at com.google.cloud.tools.jib/com.google.cloud.tools.jib.event.EventHandlersTest.testAdd(EventHandlersTest.java:67)
        Caused by:
        java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in java.lang.CompoundEnumeration@58a4a09d
            Caused by:
            org.mockito.exceptions.base.MockitoInitializationException: 
            Could not initialize inline Byte Buddy mock maker. (This mock maker is not supported on Android.)
            Java               : 11
            JVM vendor name    : Oracle Corporation
            JVM vendor version : 11.0.1+13
            JVM name           : OpenJDK 64-Bit Server VM
            JVM version        : 11.0.1+13
            JVM info           : mixed mode
            OS name            : Linux
            OS version         : 4.18.10-1rodete2-amd64
                Caused by:
                java.lang.NoClassDefFoundError: net/bytebuddy/agent/ByteBuddyAgent
                    Caused by:
                    java.lang.ClassNotFoundException: net.bytebuddy.agent.ByteBuddyAgent

I then tried adding net.bytebuddy:byte-buddy-agent,

  testImplementation 'org.mockito:mockito-core:2.23.17'
  //testImplementation 'org.mockito:mockito-core:2.23.4'
  testImplementation 'net.bytebuddy:byte-buddy-agent:1.9.7'

Then this goes back to give me the exact same error as in #1483 (comment).

If you are interested, you can try this yourself with this branch of the repo, where I cut down build.gradle.

git clone https://github.com/GoogleContainerTools/jib.git && cd jib
git checkout snapshot-jib-core-java-11-test
cd jib-core
./gradlew build
          

Thanks, I will try this out some time soon. Did you however experience that this did not happen when you used Mockito as an unnamed module rather then an automatic one?

I wrote a unit test that loads Mockito and Byte Buddy as unnamed modules and those work, I really wonder why this causes a problem. Are you maybe missing the java.instrument module? Could you try adding it explicitly, maybe that is the problem.

I will also get back to you once I got to investigate your issue.

java.instrument is there. I've put --list-modules in build.gradle to print out the modules.

Successfully started process 'Gradle Test Executor 3'
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]   <-- it's there
[email protected]
[email protected]
[email protected]

Anyway, I tried explicitly adding java.instrument to --module-path, but of no avail.

test {
  doFirst {
    jvmArgs = [
        '--module-path', classpath.asPath + ':java.instrument',

Did you however experience that this did not happen when you used Mockito as an unnamed module rather then an automatic one?

Just tried it. It sort of works with 2.23.17, but I had to manually add Objenesis, byte-buddy, and byte-buddy-agent. (UPDATE: the additional libraries are probably due to #1483 (comment).) I did not need net.bytebuddy.experimental = true.

test {
  inputs.property("moduleName", moduleName)
  doFirst {
    jvmArgs = [
       // Take out [email protected] from module path.
       // "mockito-libs/mockito-core-2.23.17.jar" is a JAR that I built locally from Mockito source repo.
       '--module-path', classpath.minus(files('mockito-libs/mockito-core-2.23.17.jar')).asPath,
   classpath = files(sourceSets.test.output.resourcesDir,
       // Add it to class path instead.
       'mockito-libs/mockito-core-2.23.17.jar')

Before starting, I just opened and exported every package temporarily just for my convenience. I'll need to figure out what to do ultimately.

open /* opens everything */ module com.google.cloud.tools.jib {
  exports com.google.cloud.tools.jib;
  exports com.google.cloud.tools.jib.api;
  exports ...
  ... (exports everything) ...

As above, I removed [email protected] from the module path and added the JAR in the class path. It gave me

com.google.cloud.tools.jib.builder.steps.RetrieveRegistryCredentialsStepTest > classMethod FAILED
    java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
        Caused by:
        java.lang.NoClassDefFoundError: net/bytebuddy/TypeCache
            Caused by:
            java.lang.ClassNotFoundException: net.bytebuddy.TypeCache

I added

  testImplementation 'net.bytebuddy:byte-buddy:1.9.7'

Then it gave me

    java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
        Caused by:
        java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in java.lang.CompoundEnumeration@221bceb7
            Caused by:
            org.mockito.exceptions.base.MockitoInitializationException:
            Could not initialize inline Byte Buddy mock maker. (This mock maker is not supported on Android.)
            Java               : 11
            JVM vendor name    : Oracle Corporation
            JVM vendor version : 11.0.1+13
            JVM name           : OpenJDK 64-Bit Server VM
            JVM version        : 11.0.1+13
            JVM info           : mixed mode
            OS name            : Linux
            OS version         : 4.18.10-1rodete2-amd64
                Caused by:
                java.lang.NoClassDefFoundError: net/bytebuddy/agent/ByteBuddyAgent
                    Caused by:
                    java.lang.ClassNotFoundException: net.bytebuddy.agent.ByteBuddyAgent

I added

  testImplementation 'net.bytebuddy:byte-buddy-agent:1.9.7'

Then it gave me

    java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.InstantiatorProvider2 (alternate: interface org.mockito.plugins.InstantiatorProvider)
        Caused by:
        java.lang.NoClassDefFoundError: org/objenesis/ObjenesisStd
            Caused by:
            java.lang.ClassNotFoundException: org.objenesis.ObjenesisStd

I added

  testImplementation 'org.objenesis:objenesis:3.0.1'

Now it works.

> Task :test
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
BUILD SUCCESSFUL in 7s
7 actionable tasks: 7 executed
          

It is strange that Gradle does not detect those dependencies. They are published correctly in the POM.

As for your problem, I debug this and found a strange behavior of Class.forName:

If I call the simple version of the method, I cannot locate the class.
If I call the version of the method and specify the bootstrap class loader, I can locate the class and everything works as expected.

This is an easy fix but I do not understand the behavior at the moment and need to investigate a bit.

Ah, thanks again for looking into it.

It is strange that Gradle does not detect those dependencies. They are published correctly in the POM.

Could this be because I added Mockito 2.23.17 from a file like below? (I'm quite new to Gradle.)

 repositories {
   flatDir {
     dirs 'mockito-libs' // to load Mockito 2.23.17 built locally from source
   mavenCentral()

(I put the JAR at <project root>/mockito-libs/mockito-core-2.23.17.jar.)

If I call the simple version of the method, I cannot locate the class.
If I call the version of the method and specify the bootstrap class loader, I can locate the class and everything works as expected.

Being on a module path, could this be related to module encapsulation? For example, I know Class.getResource() works differently on Java 9+ with the new resource encapsulation (e.g., cannot get resources in a non-root level if not opened by the module).

I realize now what the issue is. With Mockito being loaded as an automatic module, it is managed by the new BuiltIn class loader. The class loader does not consider packages outside of its own class loader if a package is already assigned to a module that this class loader owns. When injecting the class, the "bytebuddy" package already belongs to the class loader in question and the package in the bootloader is not checked. If I change the check, the class is found but no class injected into the bootstrap loader will be able to load it. The solution must therefore be to move the class into a dedicated package.

Had to do the following:

testImplementation (group: 'org.mockito', name: 'mockito-core', version: '2.24.0') {
    exclude group: 'net.bytebuddy'
testImplementation ("net.bytebuddy:byte-buddy:1.9.13")

This doesn't work either. Mockito-core 2.23.4, byte buddy 1.9.16, tried -Dnet.bytebuddy.experimental=true that makes no difference, OpenJDK 11.0.6. The mockito-core version is obtained from Spring Boot parent POM. Tried to use a different version, seems not to work, maybe I need to get rid of the parent POM and declare all the versions of everything manually?

Without Jacoco, it works correctly, all tests passing. If Jacoco is used, I'm getting the error, cannot create any mocks. jacoco-maven-plugin 0.8.5.

Thanks for the reply. It ended up working after updating mockito-core to the latest 2.x version. Le sam. 18 avr. 2020 à 17:54, Rafael Winterhalter <[email protected]> a écrit :
DevX has recommended that teams stop using Java 8 (see
https://docs.google.com/document/d/1ZR-YnaXCT5_gLVmTCeGs0mWd3KPaAozPjQK8uUzHZ9w/edit )
so it's good to get this project capable of running tests under Java 11!
The only blocker is the old version of Mockito, which would throw this error
when running tests under Java 11:
java.lang.UnsupportedOperationException: Cannot define class using reflection: Could not find sun.misc.Unsafe
It looks like Java 11 compatability was added to Mockito sometime around 2019
(see mockito/mockito#1483 and
https://github.com/mockito/mockito/blob/release/2.x/doc/release-notes/official.md)
so just upgrading to the latest version fixes the issue.
There's also a couple of changes to mocking-exceptions in the tests, as the later
version of Mockito is more strict - you can't say `thenThrow(classOf[Exception])`
if the exception is a checked-exception, and method you're mocking doesn't
actually throw that exception! So instead we just say `thenThrow(classOf[RuntimeException])`,
which is something any method can do at anytime, without having to declare it
in its signature.
[info] - should succeed with NO_CONTENT *** FAILED ***
[info]   org.mockito.exceptions.base.MockitoException: Mockito cannot mock this class: interface com.gu.atom.data.PreviewDataStore
[info]
[info] Mockito can only mock visible & non-final classes.
[info] If you're not sure why you're getting this error, please report to the mailing list.
[info]
[info] Underlying exception : java.lang.UnsupportedOperationException: Cannot define class using reflection: Could not find sun.misc.Unsafe
[info]   at org.scalatest.mockito.MockitoSugar.mock(MockitoSugar.scala:73)
[info]   at org.scalatest.mockito.MockitoSugar.mock$(MockitoSugar.scala:72)
[info]   at org.scalatest.mockito.MockitoSugar$.mock(MockitoSugar.scala:155)
[info]   at com.gu.atom.play.test.AtomSuite.previewDataStoreMockWithTestData(AtomSuite.scala:22)
[info]   at com.gu.atom.play.test.AtomSuite.previewDataStoreMockWithTestData$(AtomSuite.scala:21)
[info]   at com.gu.atom.play.test.AtomAPIActionsSpec.previewDataStoreMockWithTestData(AtomAPIActionsSpec.scala:15)
[info]   at com.gu.atom.play.test.AtomSuite.initialPreviewDataStore(AtomSuite.scala:53)
[info]   at com.gu.atom.play.test.AtomSuite.initialPreviewDataStore$(AtomSuite.scala:53)
[info]   at com.gu.atom.play.test.AtomAPIActionsSpec.initialPreviewDataStore(AtomAPIActionsSpec.scala:15)
[info]   at com.gu.atom.play.test.AtomSuite$AtomTestConf$.apply$default$1(AtomSuite.scala:73)
[info]   ...
[info]   Cause: java.lang.UnsupportedOperationException: Cannot define class using reflection: Could not find sun.misc.Unsafe
[info]   at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection$Dispatcher$Initializable$Unavailable.defineClass(ClassInjector.java:334)
[info]   at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection.inject(ClassInjector.java:187)
[info]   at net.bytebuddy.dynamic.loading.ClassLoadingStrategy$Default$InjectionDispatcher.load(ClassLoadingStrategy.java:205)
[info]   at net.bytebuddy.dynamic.TypeResolutionStrategy$Passive.initialize(TypeResolutionStrategy.java:79)
[info]   at net.bytebuddy.dynamic.DynamicType$Default$Unloaded.load(DynamicType.java:4247)
[info]   at org.mockito.internal.creation.bytebuddy.MockBytecodeGenerator.generateMockClass(MockBytecodeGenerator.java:60)
[info]   at org.mockito.internal.creation.bytebuddy.CachingMockBytecodeGenerator$CachedBytecodeGenerator.generate(CachingMockBytecodeGenerator.java:99)
[info]   at org.mockito.internal.creation.bytebuddy.CachingMockBytecodeGenerator$CachedBytecodeGenerator.getOrGenerateMockClass(CachingMockBytecodeGenerator.java:89)
[info]   at org.mockito.internal.creation.bytebuddy.CachingMockBytecodeGenerator.get(CachingMockBytecodeGenerator.java:37)
[info]   at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.createProxyClass(ByteBuddyMockMaker.java:54)
[info]   ...
      
DevX has recommended that teams stop using Java 8 (see
https://docs.google.com/document/d/1ZR-YnaXCT5_gLVmTCeGs0mWd3KPaAozPjQK8uUzHZ9w/edit )
so it's good to get this project capable of running tests under Java 11!
The only blocker is the old version of Mockito, which would throw this error
when running tests under Java 11:
java.lang.UnsupportedOperationException: Cannot define class using reflection: Could not find sun.misc.Unsafe
It looks like Java 11 compatability was added to Mockito sometime around 2019
(see mockito/mockito#1483 and
https://github.com/mockito/mockito/blob/release/2.x/doc/release-notes/official.md)
so just upgrading to the latest version fixes the issue.
There's also a couple of changes to mocking-exceptions in the tests, as the later
version of Mockito is more strict - you can't say `thenThrow(classOf[Exception])`
if the exception is a checked-exception, and method you're mocking doesn't
actually throw that exception! So instead we just say `thenThrow(classOf[RuntimeException])`,
which is something any method can do at anytime, without having to declare it
in its signature.
[info] - should succeed with NO_CONTENT *** FAILED ***
[info]   org.mockito.exceptions.base.MockitoException: Mockito cannot mock this class: interface com.gu.atom.data.PreviewDataStore
[info]
[info] Mockito can only mock visible & non-final classes.
[info] If you're not sure why you're getting this error, please report to the mailing list.
[info]
[info] Underlying exception : java.lang.UnsupportedOperationException: Cannot define class using reflection: Could not find sun.misc.Unsafe
[info]   at org.scalatest.mockito.MockitoSugar.mock(MockitoSugar.scala:73)
[info]   at org.scalatest.mockito.MockitoSugar.mock$(MockitoSugar.scala:72)
[info]   at org.scalatest.mockito.MockitoSugar$.mock(MockitoSugar.scala:155)
[info]   at com.gu.atom.play.test.AtomSuite.previewDataStoreMockWithTestData(AtomSuite.scala:22)
[info]   at com.gu.atom.play.test.AtomSuite.previewDataStoreMockWithTestData$(AtomSuite.scala:21)
[info]   at com.gu.atom.play.test.AtomAPIActionsSpec.previewDataStoreMockWithTestData(AtomAPIActionsSpec.scala:15)
[info]   at com.gu.atom.play.test.AtomSuite.initialPreviewDataStore(AtomSuite.scala:53)
[info]   at com.gu.atom.play.test.AtomSuite.initialPreviewDataStore$(AtomSuite.scala:53)
[info]   at com.gu.atom.play.test.AtomAPIActionsSpec.initialPreviewDataStore(AtomAPIActionsSpec.scala:15)
[info]   at com.gu.atom.play.test.AtomSuite$AtomTestConf$.apply$default$1(AtomSuite.scala:73)
[info]   ...
[info]   Cause: java.lang.UnsupportedOperationException: Cannot define class using reflection: Could not find sun.misc.Unsafe
[info]   at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection$Dispatcher$Initializable$Unavailable.defineClass(ClassInjector.java:334)
[info]   at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection.inject(ClassInjector.java:187)
[info]   at net.bytebuddy.dynamic.loading.ClassLoadingStrategy$Default$InjectionDispatcher.load(ClassLoadingStrategy.java:205)
[info]   at net.bytebuddy.dynamic.TypeResolutionStrategy$Passive.initialize(TypeResolutionStrategy.java:79)
[info]   at net.bytebuddy.dynamic.DynamicType$Default$Unloaded.load(DynamicType.java:4247)
[info]   at org.mockito.internal.creation.bytebuddy.MockBytecodeGenerator.generateMockClass(MockBytecodeGenerator.java:60)
[info]   at org.mockito.internal.creation.bytebuddy.CachingMockBytecodeGenerator$CachedBytecodeGenerator.generate(CachingMockBytecodeGenerator.java:99)
[info]   at org.mockito.internal.creation.bytebuddy.CachingMockBytecodeGenerator$CachedBytecodeGenerator.getOrGenerateMockClass(CachingMockBytecodeGenerator.java:89)
[info]   at org.mockito.internal.creation.bytebuddy.CachingMockBytecodeGenerator.get(CachingMockBytecodeGenerator.java:37)
[info]   at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.createProxyClass(ByteBuddyMockMaker.java:54)
[info]   ...
      
DevX has recommended that teams stop using Java 8 (see
https://docs.google.com/document/d/1ZR-YnaXCT5_gLVmTCeGs0mWd3KPaAozPjQK8uUzHZ9w/edit )
so it's good to get this project capable of running tests under Java 11!
The only blocker is the old version of Mockito, which would throw this error
when running tests under Java 11:
java.lang.UnsupportedOperationException: Cannot define class using reflection: Could not find sun.misc.Unsafe
It looks like Java 11 compatability was added to Mockito sometime around 2019
(see mockito/mockito#1483 and
https://github.com/mockito/mockito/blob/release/2.x/doc/release-notes/official.md)
so just upgrading to the latest version fixes the issue.
There's also a couple of changes to mocking-exceptions in the tests, as the later
version of Mockito is more strict - you can't say `thenThrow(classOf[Exception])`
if the exception is a checked-exception, and method you're mocking doesn't
actually throw that exception! So instead we just say `thenThrow(classOf[RuntimeException])`,
which is something any method can do at anytime, without having to declare it
in its signature.
[info] - should succeed with NO_CONTENT *** FAILED ***
[info]   org.mockito.exceptions.base.MockitoException: Mockito cannot mock this class: interface com.gu.atom.data.PreviewDataStore
[info]
[info] Mockito can only mock visible & non-final classes.
[info] If you're not sure why you're getting this error, please report to the mailing list.
[info]
[info] Underlying exception : java.lang.UnsupportedOperationException: Cannot define class using reflection: Could not find sun.misc.Unsafe
[info]   at org.scalatest.mockito.MockitoSugar.mock(MockitoSugar.scala:73)
[info]   at org.scalatest.mockito.MockitoSugar.mock$(MockitoSugar.scala:72)
[info]   at org.scalatest.mockito.MockitoSugar$.mock(MockitoSugar.scala:155)
[info]   at com.gu.atom.play.test.AtomSuite.previewDataStoreMockWithTestData(AtomSuite.scala:22)
[info]   at com.gu.atom.play.test.AtomSuite.previewDataStoreMockWithTestData$(AtomSuite.scala:21)
[info]   at com.gu.atom.play.test.AtomAPIActionsSpec.previewDataStoreMockWithTestData(AtomAPIActionsSpec.scala:15)
[info]   at com.gu.atom.play.test.AtomSuite.initialPreviewDataStore(AtomSuite.scala:53)
[info]   at com.gu.atom.play.test.AtomSuite.initialPreviewDataStore$(AtomSuite.scala:53)
[info]   at com.gu.atom.play.test.AtomAPIActionsSpec.initialPreviewDataStore(AtomAPIActionsSpec.scala:15)
[info]   at com.gu.atom.play.test.AtomSuite$AtomTestConf$.apply$default$1(AtomSuite.scala:73)
[info]   ...
[info]   Cause: java.lang.UnsupportedOperationException: Cannot define class using reflection: Could not find sun.misc.Unsafe
[info]   at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection$Dispatcher$Initializable$Unavailable.defineClass(ClassInjector.java:334)
[info]   at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection.inject(ClassInjector.java:187)
[info]   at net.bytebuddy.dynamic.loading.ClassLoadingStrategy$Default$InjectionDispatcher.load(ClassLoadingStrategy.java:205)
[info]   at net.bytebuddy.dynamic.TypeResolutionStrategy$Passive.initialize(TypeResolutionStrategy.java:79)
[info]   at net.bytebuddy.dynamic.DynamicType$Default$Unloaded.load(DynamicType.java:4247)
[info]   at org.mockito.internal.creation.bytebuddy.MockBytecodeGenerator.generateMockClass(MockBytecodeGenerator.java:60)
[info]   at org.mockito.internal.creation.bytebuddy.CachingMockBytecodeGenerator$CachedBytecodeGenerator.generate(CachingMockBytecodeGenerator.java:99)
[info]   at org.mockito.internal.creation.bytebuddy.CachingMockBytecodeGenerator$CachedBytecodeGenerator.getOrGenerateMockClass(CachingMockBytecodeGenerator.java:89)
[info]   at org.mockito.internal.creation.bytebuddy.CachingMockBytecodeGenerator.get(CachingMockBytecodeGenerator.java:37)
[info]   at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.createProxyClass(ByteBuddyMockMaker.java:54)
[info]   ...
      
DevX has recommended that teams stop using Java 8 (see
https://docs.google.com/document/d/1ZR-YnaXCT5_gLVmTCeGs0mWd3KPaAozPjQK8uUzHZ9w/edit )
so it's good to get this project capable of running tests under Java 11!
The only blocker is the old version of Mockito, which would throw this error
when running tests under Java 11:
java.lang.UnsupportedOperationException: Cannot define class using reflection: Could not find sun.misc.Unsafe
It looks like Java 11 compatability was added to Mockito sometime around 2019
(see mockito/mockito#1483 and
https://github.com/mockito/mockito/blob/release/2.x/doc/release-notes/official.md)
so just upgrading to the latest version fixes the issue.
There's also a couple of changes to mocking-exceptions in the tests, as the later
version of Mockito is more strict - you can't say `thenThrow(classOf[Exception])`
if the exception is a checked-exception, and method you're mocking doesn't
actually throw that exception! So instead we just say `thenThrow(classOf[RuntimeException])`,
which is something any method can do at anytime, without having to declare it
in its signature.
[info] - should succeed with NO_CONTENT *** FAILED ***
[info]   org.mockito.exceptions.base.MockitoException: Mockito cannot mock this class: interface com.gu.atom.data.PreviewDataStore
[info]
[info] Mockito can only mock visible & non-final classes.
[info] If you're not sure why you're getting this error, please report to the mailing list.
[info]
[info] Underlying exception : java.lang.UnsupportedOperationException: Cannot define class using reflection: Could not find sun.misc.Unsafe
[info]   at org.scalatest.mockito.MockitoSugar.mock(MockitoSugar.scala:73)
[info]   at org.scalatest.mockito.MockitoSugar.mock$(MockitoSugar.scala:72)
[info]   at org.scalatest.mockito.MockitoSugar$.mock(MockitoSugar.scala:155)
[info]   at com.gu.atom.play.test.AtomSuite.previewDataStoreMockWithTestData(AtomSuite.scala:22)
[info]   at com.gu.atom.play.test.AtomSuite.previewDataStoreMockWithTestData$(AtomSuite.scala:21)
[info]   at com.gu.atom.play.test.AtomAPIActionsSpec.previewDataStoreMockWithTestData(AtomAPIActionsSpec.scala:15)
[info]   at com.gu.atom.play.test.AtomSuite.initialPreviewDataStore(AtomSuite.scala:53)
[info]   at com.gu.atom.play.test.AtomSuite.initialPreviewDataStore$(AtomSuite.scala:53)
[info]   at com.gu.atom.play.test.AtomAPIActionsSpec.initialPreviewDataStore(AtomAPIActionsSpec.scala:15)
[info]   at com.gu.atom.play.test.AtomSuite$AtomTestConf$.apply$default$1(AtomSuite.scala:73)
[info]   ...
[info]   Cause: java.lang.UnsupportedOperationException: Cannot define class using reflection: Could not find sun.misc.Unsafe
[info]   at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection$Dispatcher$Initializable$Unavailable.defineClass(ClassInjector.java:334)
[info]   at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection.inject(ClassInjector.java:187)
[info]   at net.bytebuddy.dynamic.loading.ClassLoadingStrategy$Default$InjectionDispatcher.load(ClassLoadingStrategy.java:205)
[info]   at net.bytebuddy.dynamic.TypeResolutionStrategy$Passive.initialize(TypeResolutionStrategy.java:79)
[info]   at net.bytebuddy.dynamic.DynamicType$Default$Unloaded.load(DynamicType.java:4247)
[info]   at org.mockito.internal.creation.bytebuddy.MockBytecodeGenerator.generateMockClass(MockBytecodeGenerator.java:60)
[info]   at org.mockito.internal.creation.bytebuddy.CachingMockBytecodeGenerator$CachedBytecodeGenerator.generate(CachingMockBytecodeGenerator.java:99)
[info]   at org.mockito.internal.creation.bytebuddy.CachingMockBytecodeGenerator$CachedBytecodeGenerator.getOrGenerateMockClass(CachingMockBytecodeGenerator.java:89)
[info]   at org.mockito.internal.creation.bytebuddy.CachingMockBytecodeGenerator.get(CachingMockBytecodeGenerator.java:37)
[info]   at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.createProxyClass(ByteBuddyMockMaker.java:54)
[info]   ...
      
DevX has recommended that teams stop using Java 8 (see
https://docs.google.com/document/d/1ZR-YnaXCT5_gLVmTCeGs0mWd3KPaAozPjQK8uUzHZ9w/edit )
so it's good to get this project capable of running tests under Java 11!
The only blocker is the old version of Mockito, which would throw this error
when running tests under Java 11:
java.lang.UnsupportedOperationException: Cannot define class using reflection: Could not find sun.misc.Unsafe
It looks like Java 11 compatability was added to Mockito sometime around 2019
(see mockito/mockito#1483 and
https://github.com/mockito/mockito/blob/release/2.x/doc/release-notes/official.md)
so just upgrading to the latest version fixes the issue.
There's also a couple of changes to mocking-exceptions in the tests, as the later
version of Mockito is more strict - you can't say `thenThrow(classOf[Exception])`
if the exception is a checked-exception, and method you're mocking doesn't
actually throw that exception! So instead we just say `thenThrow(classOf[RuntimeException])`,
which is something any method can do at anytime, without having to declare it
in its signature.
[info] - should succeed with NO_CONTENT *** FAILED ***
[info]   org.mockito.exceptions.base.MockitoException: Mockito cannot mock this class: interface com.gu.atom.data.PreviewDataStore
[info]
[info] Mockito can only mock visible & non-final classes.
[info] If you're not sure why you're getting this error, please report to the mailing list.
[info]
[info] Underlying exception : java.lang.UnsupportedOperationException: Cannot define class using reflection: Could not find sun.misc.Unsafe
[info]   at org.scalatest.mockito.MockitoSugar.mock(MockitoSugar.scala:73)
[info]   at org.scalatest.mockito.MockitoSugar.mock$(MockitoSugar.scala:72)
[info]   at org.scalatest.mockito.MockitoSugar$.mock(MockitoSugar.scala:155)
[info]   at com.gu.atom.play.test.AtomSuite.previewDataStoreMockWithTestData(AtomSuite.scala:22)
[info]   at com.gu.atom.play.test.AtomSuite.previewDataStoreMockWithTestData$(AtomSuite.scala:21)
[info]   at com.gu.atom.play.test.AtomAPIActionsSpec.previewDataStoreMockWithTestData(AtomAPIActionsSpec.scala:15)
[info]   at com.gu.atom.play.test.AtomSuite.initialPreviewDataStore(AtomSuite.scala:53)
[info]   at com.gu.atom.play.test.AtomSuite.initialPreviewDataStore$(AtomSuite.scala:53)
[info]   at com.gu.atom.play.test.AtomAPIActionsSpec.initialPreviewDataStore(AtomAPIActionsSpec.scala:15)
[info]   at com.gu.atom.play.test.AtomSuite$AtomTestConf$.apply$default$1(AtomSuite.scala:73)
[info]   ...
[info]   Cause: java.lang.UnsupportedOperationException: Cannot define class using reflection: Could not find sun.misc.Unsafe
[info]   at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection$Dispatcher$Initializable$Unavailable.defineClass(ClassInjector.java:334)
[info]   at net.bytebuddy.dynamic.loading.ClassInjector$UsingReflection.inject(ClassInjector.java:187)
[info]   at net.bytebuddy.dynamic.loading.ClassLoadingStrategy$Default$InjectionDispatcher.load(ClassLoadingStrategy.java:205)
[info]   at net.bytebuddy.dynamic.TypeResolutionStrategy$Passive.initialize(TypeResolutionStrategy.java:79)
[info]   at net.bytebuddy.dynamic.DynamicType$Default$Unloaded.load(DynamicType.java:4247)
[info]   at org.mockito.internal.creation.bytebuddy.MockBytecodeGenerator.generateMockClass(MockBytecodeGenerator.java:60)
[info]   at org.mockito.internal.creation.bytebuddy.CachingMockBytecodeGenerator$CachedBytecodeGenerator.generate(CachingMockBytecodeGenerator.java:99)
[info]   at org.mockito.internal.creation.bytebuddy.CachingMockBytecodeGenerator$CachedBytecodeGenerator.getOrGenerateMockClass(CachingMockBytecodeGenerator.java:89)
[info]   at org.mockito.internal.creation.bytebuddy.CachingMockBytecodeGenerator.get(CachingMockBytecodeGenerator.java:37)
[info]   at org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker.createProxyClass(ByteBuddyMockMaker.java:54)
[info]   ...