question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JDK 16 & InaccessibleObjectException

See original GitHub issue

Expected Behavior

I upgraded to JDK16 & Gradle 7 and I have issues when running mockk:

// method for mocking timestamps
fun mockNow(string: String) {
    mockkStatic(Instant::class, ZonedDateTime::class, LocalDate::class, LocalDateTime::class)
    every { Instant.now() }.returns(instantOf(string))
    every { ZonedDateTime.now() }.returns(zonedDateTimeOf(string))
}

mockNow("2020-01-01T12:00:00Z")

Will fail with:

java.time.format.DateTimeParseException: Text '2020-01-01T12:00:00Z' could not be parsed: Unable to make private static java.time.LocalDate java.time.LocalDate.create(int,int,int) accessible: module java.base does not "opens java.time" to unnamed module @7254bd8e
        at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:2023)
        at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1958)
        at java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:600)

(see full stacktrace below)

Current Behavior

We currently use JDK11 where this works correctly.

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.

  • MockK version: 1.12.0
  • OS: Any
  • Kotlin version: 1.5.30-RC
  • JDK version: 16
  • JUnit version: 5.7.0
  • Type of test: unit test

Full stacktrace

java.time.format.DateTimeParseException: Text '2020-01-01T12:00:00Z' could not be parsed: Unable to make private static java.time.LocalDate java.time.LocalDate.create(int,int,int) accessible: module java.base does not "opens java.time" to unnamed module @7254bd8e
        at java.base/java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:2023)
        at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1958)
        at java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:600)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:567)
        at io.mockk.proxy.jvm.advice.MethodCall.call(MethodCall.kt:14)
        at io.mockk.proxy.jvm.advice.SelfCallEliminatorCallable.call(SelfCallEliminatorCallable.kt:14)
        at io.mockk.impl.instantiation.JvmMockFactoryHelper.handleOriginalCall(JvmMockFactoryHelper.kt:95)
        at io.mockk.impl.instantiation.JvmMockFactoryHelper.access$handleOriginalCall(JvmMockFactoryHelper.kt:18)
        at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1$invocation$$inlined$stdFunctions$lambda$1.invoke(JvmMockFactoryHelper.kt:27)
        at io.mockk.impl.stub.MockKStub$handleInvocation$originalPlusToString$1.invoke(MockKStub.kt:230)
        at io.mockk.impl.stub.SpyKStub.defaultAnswer(SpyKStub.kt:15)
        at io.mockk.impl.stub.MockKStub.answer(MockKStub.kt:42)
        at io.mockk.impl.recording.states.AnsweringState.call(AnsweringState.kt:16)
        at io.mockk.impl.recording.CommonCallRecorder.call(CommonCallRecorder.kt:53)
        at io.mockk.impl.stub.MockKStub.handleInvocation(MockKStub.kt:266)
        at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation(JvmMockFactoryHelper.kt:23)
        at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:21)
        at java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:600)
        at java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:585)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:567)
        at io.mockk.proxy.jvm.advice.MethodCall.call(MethodCall.kt:14)
        at io.mockk.proxy.jvm.advice.SelfCallEliminatorCallable.call(SelfCallEliminatorCallable.kt:14)
        at io.mockk.impl.instantiation.JvmMockFactoryHelper.handleOriginalCall(JvmMockFactoryHelper.kt:95)
        at io.mockk.impl.instantiation.JvmMockFactoryHelper.access$handleOriginalCall(JvmMockFactoryHelper.kt:18)
        at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1$invocation$$inlined$stdFunctions$lambda$1.invoke(JvmMockFactoryHelper.kt:27)
        at io.mockk.impl.stub.MockKStub$handleInvocation$originalPlusToString$1.invoke(MockKStub.kt:230)
        at io.mockk.impl.stub.SpyKStub.defaultAnswer(SpyKStub.kt:15)
        at io.mockk.impl.stub.MockKStub.answer(MockKStub.kt:42)
        at io.mockk.impl.recording.states.AnsweringState.call(AnsweringState.kt:16)
        at io.mockk.impl.recording.CommonCallRecorder.call(CommonCallRecorder.kt:53)
        at io.mockk.impl.stub.MockKStub.handleInvocation(MockKStub.kt:266)
        at io.mockk.impl.instantiation.JvmMockFactoryHelper$mockHandler$1.invocation(JvmMockFactoryHelper.kt:23)
        at io.mockk.proxy.jvm.advice.Interceptor.call(Interceptor.kt:21)
        at java.base/java.time.ZonedDateTime.parse(ZonedDateTime.java:585)
        at net.goout.instant.InstantUtilsKt.zonedDateTimeOf(InstantUtils.kt:63)
        at net.goout.instant.InstantUtilsKt.instantOf(InstantUtils.kt:79)
        at net.goout.instant.InstantTestUtilsKt.mockNow(InstantTestUtils.kt:14)
        at net.goout.instant.InstantUtilsTest.testMockNow(InstantUtilsTest.kt:24)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

23reactions
knyttlcommented, Nov 16, 2021
tasks.test {
        …
        // https://github.com/mockk/mockk/issues/681
        jvmArgs("--add-opens", "java.base/java.time=ALL-UNNAMED")
}
3reactions
timkantecommented, Nov 3, 2021

You can fix this issue on your own.

To the java Test-Process, add --add-opens java.base/java.time=ALL-UNNAMED as start-argument and it’ll work. Here is the corresponding JEP for reference: https://openjdk.java.net/jeps/396

If you’d have shared your grade config, as required by the issue-template, I could tell you exactly, but as you did not provide it, you’ll have to figure out where exactly to add it on your own.

This is not a bug in MockK.

Read more comments on GitHub >

github_iconTop Results From Across the Web

InaccessibleObjectException (Java SE 16 & JDK 16)
Constructs an InaccessibleObjectException with the given detail message. Method Summary. Methods declared in class java.lang.Throwable · addSuppressed, ...
Read more >
InaccessibleObjectException with JDK 16 · Issue #1875 - GitHub
GSon (2.8.6) has trouble with JDK 16, seemingly due to ... InaccessibleObjectException: Unable to make private java.util.
Read more >
How to resolve "InaccessibleObjectException" of jdk16
java, I found cglib is trying to use "defineClass" of "sun.misc.Unsafe". Unfortunately, I can find that class but cannot find that method in...
Read more >
Build fails with JDK-16 RC2 (InaccessibleObjectException)
Use the newest EAP Idea and JDK-16 Release candidate 2; Make changes to a project; Run it, triggering an automatic build. What is...
Read more >
How to run FisPro with JDK >= 16
The JDK 16 has changed the default access to some java packages to deny – this ... InaccessibleObjectException: Unable to make field protected...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found