New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
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
Java 11 compatibility with inline mocks #1483
Comments
|
@raphw, can you chime in? Thanks! |
|
Run it with -Dnet.bytebuddy.experimental=true. Its necessary for now as this might break for any new EA release if last minute changes are applied. Use at own risk after any EA update. |
|
I have the same problem on Java 10 (and not Java 8) with mocked class coming from Kotlin code. Using the 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> |
|
I just sent a PR for Byte Buddy that should fix Java 11 support. |
|
But we probably need to update Objenesis, too. |
|
Just tested Mockito 2.22.0 and Byte Buddy 1.8.22 with inline mocks using build |
|
Good to hear. Still waiting for ASM to reach 7.0, then I am spinning up a release and patch Mockito upstream. |
|
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: |
|
Byte Buddy already used the beta but I updated today. Thanks for the ping. |
|
Any idea when we should expect this to work again without having to use |
|
Could you check your class path to see if there is an old version of Byte Buddy somewhere on it? Byte Buddy 1.9.0 which comes with Mockito should do just fine with Java 11. |
|
You're right, I have older versions being used elsewhere in my project that must have been pulled in. I was able to get it to work by explicitly adding byte-buddy 1.9.0 to my pom.xml. Thanks! |
|
Trying to migrate a Gradle project from Java 8 to Java 11 (with the new Isn't |
|
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 |
|
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 I then tried adding 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 |
|
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 I will also get back to you once I got to investigate your issue. |
|
Anyway, I tried explicitly adding test {
...
doFirst {
jvmArgs = [
'--module-path', classpath.asPath + ':java.instrument', |
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 test {
inputs.property("moduleName", moduleName)
doFirst {
jvmArgs = [
// Take out org.mockito@2.23.17 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 I added testImplementation 'net.bytebuddy:byte-buddy:1.9.7'Then it gave me I added testImplementation 'net.bytebuddy:byte-buddy-agent:1.9.7'Then it gave me I added testImplementation 'org.objenesis:objenesis:3.0.1'Now it works. |
|
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 If I call the simple version of the method, I cannot locate the class. 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.
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 |
|
Probably, I assume this causes Mockito being added as a jar file direcly rather then it being resolved using a repository that would included this additional meta data. |
Being on a module path, could this be related to module encapsulation? For example, I know |
|
This was my first suspicion but the semantics of |
|
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") |
|
Hi, |
|
Hi, 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. |
|
It should work but maybe Jacoco breaks something. What error message are you getting? |
|
Hi,
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 <notifications@github.com>
a écrit :
… It should work but maybe Jacoco breaks something. What error message are
you getting?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#1483 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AC5MYINEICNLDWOYLNF3ONLRNIORVANCNFSM4FQ6SITQ>
.
--
/------------------------------------------------------------------------
/ Eric Buist <buisteric@gmail.com <me@ericbuist.com>>
/ Ph.D en informatique
/ Ingénieur de recherche en logiciels
/ Nuance communications
/ http://www.ericbuist.com
/----------------------------------------------------------------------
|
|
Closing this issue as fixed with our recent versions of Mockito (which supports up to JDK 17 at the time of writing). |
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] ... ```
Java 11 compatibility was addressed under issue #1419, however with inline mocks (required to mock private/final classes) it is still not working. Please see comments under issue #1419.
The text was updated successfully, but these errors were encountered: