QuarkusTest with Mongo and PanacheMock in Kotlin failing on Mokito.when

Viewed 17

I am working on a Quarkus project with MongoDB and Kotlin using PanacheMongoEntity.

The Problem:

Mockito.when throws an error when using with PanacheMock.mock

The error:

when() requires an argument which has to be 'a method call on a mock'.

See the error logs (at bottom) for more info.

Question:

Is it even possible to do it this way in Quarkus-Kotlin? The documentation at https://quarkus.io/guides/mongodb-panache-kotlin does not cover the testing part, it just refers to the java examples.

Code examples

Dependencies (Gradle)
dependencies {
    implementation enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}")
    implementation 'io.quarkus:quarkus-arc'
    implementation 'io.quarkus:quarkus-kotlin'
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
    implementation 'io.quarkus:quarkus-resteasy-jackson'
    implementation 'io.quarkus:quarkus-mongodb-panache-kotlin'
    implementation 'io.quarkus:quarkus-security-jpa'
    // TESTING
    testImplementation 'io.quarkus:quarkus-junit5'
    testImplementation 'io.rest-assured:rest-assured'
    testImplementation 'io.quarkus:quarkus-panache-mock'
    // makes final classes testable remove when mockito core implemented
    testImplementation 'org.mockito:mockito-inline'
    testImplementation 'io.rest-assured:kotlin-extensions'
}
The entity
@MongoEntity(collection = "anything")
class Anything : PanacheMongoEntity() {
    companion object : PanacheMongoCompanion<Anything> {}

    lateinit var id: String
    lateinit var type: String
}
The test:
@QuarkusTest
class AnythingResourceTest {

    @Test
    fun testFetchAllAnythings() {
        PanacheMock.mock(Anything::class.java)

        val anything = Anything()
        anything.id = "id"
        anything.type = "type"

        Mockito.`when`(Anything.Companion.listAll()).thenReturn(listOf(anything))
    }
}
The resulting error log
2022-08-23 19:03:11,290 INFO  [org.mon.dri.connection] (Test worker) Opened connection [connectionId{localValue:3, serverValue:137}] to localhost:27017


when() requires an argument which has to be 'a method call on a mock'.
For example:
    when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
   Those methods *cannot* be stubbed/verified.
   Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.

org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.
For example:
    when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
   Those methods *cannot* be stubbed/verified.
   Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.

    at com.asking.AnythingResourceTest.testFetchAllAnythings(AnythingResourceTest.kt:12)
    at java.base@17.0.2/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base@17.0.2/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base@17.0.2/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base@17.0.2/java.lang.reflect.Method.invoke(Method.java:568)
0 Answers
Related