quarkiverse / quarkus-mockk

Mockk Quarkus Extension
Apache License 2.0
24 stars 10 forks source link

Support function with generic types stubbing #173

Open rgonciarz opened 1 year ago

rgonciarz commented 1 year ago

Reflection utils cannot find both bean instance and qualifier. For bean instance a problem is related with suspended Functions using generic type.

import arrow.core.Either

typealias AssignKeyboard = suspend (Computer) -> Either<KeyboardError, Unit>
typealias AssignMouse = suspend (Computer) -> Either<MouseError, Unit>

To be more precise that's the exact type:

kotlin.jvm.functions.Function2<? super rg.zerokvm.domain.model.Computer, ? super kotlin.coroutines.Continuation<? super arrow.core.Either<? extends rg.zerokvm.domain.port.api.AssignMouse, ? extends kotlin.Unit>>, ?>

Most likely (?) literal type should be used, e.g. I'm able to find my bean instance with the code:

val typeLiteral = object : TypeLiteral<AssignMouse>() {}
val beanInstance = CDI.current().select(typeLiteral, @Named("AssignMouse"))

instead:

Arc.container().instance(fieldClass, *getQualifier(field))?.get()

Below you may see the rest of the code:

class ApiConfig {

    @ApplicationScoped
    @Named("AssignMouse")
    fun assignMouseBean(): AssignMouse = // ...

    @ApplicationScoped
    @Named("AssignKeyboard")
    fun assignKeyboardBean(): AssignKeyboard = // ...

}

// ...
@Path("/api")
class AssignMouseRestApiAdapter(
    @Named("AssignMouse")
    private val assignMouse: AssignMouse,
) {

    @Path("/assigment")
    @POST
    suspend fun foo() {
        // some code
    }

}

@QuarkusTest
internal class AssignMouseRestApiAdapterTest {

    @InjectMock
    @Named("AssignMouse")
    private lateinit var assignMouse: AssignMouse,

    fun `should foo`() {
        // ...
    }

}

Also Named Qualifier is not seen for declared annotations.

glefloch commented 1 year ago

Thanks for reporting this. I think there is two separate issue. One regarding named qualifier, and the other regarding the type. I will try to fix both of them in separate pull request