quarkiverse / quarkus-mockk

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

@InjectMock does not behave like a mock created via "mockk()" #244

Open Obirah opened 10 months ago

Obirah commented 10 months ago

I am developing a Quarkus Kubernetes operator that is interacting with AWS through the AWS SDK for Java and I'm using mockk in my unit and integration tests.

One of the cases that I need to mock are calls to AWS SDK's paginators, for example: organizationsClient.listAccountsPaginator() where organizationsClient is an OrganizationsAsyncClient.

In my unit tests I create the mock via the @MockK annotation and the mocking is fairly straightfoward:

 every { organizationsClient.listAccountsPaginator() } answers { callOriginal() }
 every { organizationsClient.listAccounts(any<ListAccountsRequest>()) } returns CompletableFuture.completedFuture(ListAccountsResponse.builder().accounts(account).build())

In my integration tests (which are @QuarkusTests), if I use @InjectMock and mock the paginator calls in the same way, I get exceptions of the following kind:

io.mockk.MockKException: no answer found for: AwsClientBean_ProducerMethod_organizationsClient_162d3fc8f8a3dacbf06a8e602ca1d5cf033cb60b_ClientProxy(#1).arc$delegate()

However, if I create a bean that replaces the original bean and create my mock with mockk() my mocks are working as expected:

class AwsTestClientBean {
    @Alternative
    @Priority(1)
    @ApplicationScoped
    fun organizationsClient(): OrganizationsAsyncClient = mockk()
}
glefloch commented 8 months ago

Thanks @Obirah for reporting this. Do you have a minimal project setup that reproduce the error ? It doesn't looks like, but do you use co-routine in your application ?