Skip to content
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

JDK 16 & InaccessibleObjectException #681

Closed
vojkny opened this issue Aug 11, 2021 · 12 comments
Closed

JDK 16 & InaccessibleObjectException #681

vojkny opened this issue Aug 11, 2021 · 12 comments

Comments

@vojkny
Copy link

vojkny commented Aug 11, 2021

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)
@vojkny
Copy link
Author

vojkny commented Sep 20, 2021

reminding myself 🙏

@ferencbeutel4711
Copy link

we are facing this issue aswell :( hope this can be fixed soon

@vojkny
Copy link
Author

vojkny commented Oct 6, 2021

🙏

@HungUnicorn
Copy link

🙏

1 similar comment
@veluxer62
Copy link

🙏

@timkante
Copy link

timkante commented 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.

@vojkny
Copy link
Author

vojkny commented Nov 4, 2021

Hi, thanks for this solution, this certainly helped and solved the issue.

This is not a bug in MockK.

Maybe add it to readme/docs, so others can easily resolve this as well.

@timkante
Copy link

timkante commented Nov 4, 2021

I don't see the necessity of putting a JDK behavior into the mockK docks. This change of behavior is well documented at the JVM itself and a google-search for module xy does not "opens yz" to unnamed module xzwould've been enough to solve it.

The issue is caused by application logic and not by mockk.

@vojkny vojkny closed this as completed Nov 4, 2021
@ferencbeutel4711
Copy link

@timkante apologies, but could you maybe give a little bit more context on how to fix this? we have added the mentioned argument in our gradle.properties file as a vm argument, but are still facing this issue..

@vojkny
Copy link
Author

vojkny commented Nov 16, 2021

tasks.test {
        …
        // https://github.com/mockk/mockk/issues/681
        jvmArgs("--add-opens", "java.base/java.time=ALL-UNNAMED")
}

@s-volkov-1
Copy link
Contributor

I think this problem and workaround/solution should be stated in docs, because it's a common obstacle for JDK 16+ users.
There are a bunch of similar issues already opened, and new ones will continue to emerge.

Also, here is config for maven users:

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <argLine>
            --add-opens java.base/java.time=ALL-UNNAMED
            --add-opens java.base/java.lang.reflect=ALL-UNNAMED <!-- handy for spying spring proxies -->
          </argLine>
        </configuration>
      </plugin>

@Raibaz
Copy link
Collaborator

Raibaz commented Oct 31, 2022

Right @sergey-volkov-lm, makes sense.

Would you please open a PR to add it to the docs? 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants