kotest: SystemEnvironmentTestListener - Unable to make field private final java.util.Map java.util.Collections$UnmodifiableMap.m accessible:

Which version of Kotest are you using KoTest - 5.3

Kotlin - 1.6.20

I’m getting the following error when trying to run this code line in a WordSpec test.

class PublisherServiceTest : WordSpec() {

    private val pubSubService = install(TestContainerExtension(pubSubContainer)) {
        addExposedPort(8085)
    }
    private val address: String = "${pubSubService.host}:${pubSubService.firstMappedPort}"

    override fun listeners() = listOf(SystemEnvironmentTestListener("PUBSUB_TARGET", address)) //<<< FAILING HERE
    init {
        ...
    }
}

Error Message: Unable to make field private final java.util.Map java.util.Collections$UnmodifiableMap.m accessible: module java.base does not "opens java.util" to unnamed module @309e345f

This is directly thrown when in SystemEnviromentExtensions.kt line 99 is called:

classOfMap.getDeclaredField("m").asAccessible().get(systemEnv) as MutableMap<String, String>

method: getEditableMapOfVariables

@Suppress("UNCHECKED_CAST")
private fun getEditableMapOfVariables(): MutableMap<String, String> {
   val systemEnv = System.getenv()
   val classOfMap = systemEnv::class.java

   return classOfMap.getDeclaredField("m").asAccessible().get(systemEnv) as MutableMap<String, String>
}

Is there something I’m doing wrong?

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 18 (12 by maintainers)

Commits related to this issue

Most upvoted comments

For me, the only fix was to have the following in the build.gradle.kts:

tasks.test {
    jvmArgs("--add-opens=java.base/java.util=ALL-UNNAMED")
}

Adding the arguments to gradle.properties did not help.

I added info here on how you can work around it: https://kotest.io/docs/extensions/system_extensions.html (see the blue info box)