mockk: Bug: ClassLoaderStrategy while mocking javax.sql.DataSource

Prerequisites

  • I am running the latest version
  • I checked the documentation and found no answer
  • I checked to make sure that this issue has not already been filed

Expected Behavior

Test should run as it did with version 1.9.2

Current Behavior

Test fails:

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access using Lookup on io.mockk.proxy.jvm.ClassLoadingStrategyChooser (file:/Users/tom/.m2/repository/io/mockk/mockk-agent-jvm/1.9.3/mockk-agent-jvm-1.9.3.jar) to interface javax.sql.DataSource
WARNING: Please consider reporting this to the maintainers of io.mockk.proxy.jvm.ClassLoadingStrategyChooser
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

java.lang.NoClassDefFoundError: io/mockk/proxy/jvm/advice/jvm/JvmMockKProxyInterceptor

	at java.sql/javax.sql.DataSource$Subclass0.getConnection(Unknown Source)
// Rest removed

Also no warnings in v. 1.9.2 as opposed to the one above.

Steps to Reproduce

Try this test:

package bug

import io.mockk.every
import io.mockk.mockk
import org.junit.Test
import javax.sql.DataSource

class TestFails {
    private val dataSource = mockk<DataSource>()

    @Test
    fun failing() {
        every { dataSource.getConnection(any(), any()) } returns null
    }
}

Context

  • MockK version: 1.9.3
  • OS: MackOs Mojave v. 10.14.2
  • Kotlin version:
  • JDK version:
    • java 10.0.2 2018-07-17
    • Java™ SE Runtime Environment 18.3 (build 10.0.2+13)
    • Java HotSpot™ 64-Bit Server VM 18.3 (build 10.0.2+13, mixed mode)
  • JUnit version: 4.12
  • Type of test: Unit test

Stack trace

Run the test example above!

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 10
  • Comments: 18 (1 by maintainers)

Commits related to this issue

Most upvoted comments

Consider to re-open this issue because it still exists. Thx!

It seems to be an issue when mocking the interface DataSource. I am using Hikari as data source implementation, therefore switching the mock to private val dataSource = mockk<HikariDataSource>() fixed the problem for me.

I am not sure if this is happening to other interfaces as well and if mocking interfaces should work.

I’m experiencing the same issue after migrating to JDK 11 on Android. I’ve seen this when trying to mock javax.net.ssl.HttpsURLConnection and javax.net.ssl.SSLSocketFactory. Mockk 1.11.0.

Running into this with Java 11 and Robolectric.

    java.lang.NoClassDefFoundError: io/mockk/proxy/jvm/advice/jvm/JvmMockKProxyInterceptor
        at java.xml/org.xml.sax.Attributes$Subclass4.getLength(Unknown Source)

You can work around it until the fix has been releases by simply removing the direct reference to the DataSource interface and replace it with an indirect one.

interface MockDataSource : DataSource

private val mockDataSource = mockk<MockDataSource>()

Any news on this issue? Having the same problem with Java 11, Kotlin 1.3 and Mockk 1.9.3.

Illegal reflective access using Lookup on io.mockk.proxy.jvm.ClassLoadingStrategyChooser (file:gradle/caches/modules-2/files-2.1/io.mockk/mockk-agent-jvm/1.9.3/d6f7b4d893caf1cda2bb28ee637b247ced5d5eab/mockk-agent-jvm-1.9.3.jar) to interface org.w3c.dom.NodeList

Same test with Mockk 1.9.2 works fine.

I encountered this same error when mocking the SSLSession interface. I was able to work around it with a simple hand-coded stub.