最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

quarkus - Is it possible to use @QuarkusComponentTestunit test a @RestClient? - Stack Overflow

programmeradmin9浏览0评论

I'm using Quarkus's REST Client and I'd like to test that it serializes my queries with a correct format and parses the responses correctly. My idea is to use WireMock and test my client specifically, but I'd like to avoid @QuarkusTest. There are extensions in my application I don't wish to start because I haven't found how to mock them yet and I want my unit tests isolated:

  • I want my tests ran offline.
  • I specifically don't want to have to configure a live token to a live service in order to run my unit tests.
  • Ideally, when I'm testing just my REST client, I'd like to be able to do it outside of a Quarkus application.

I tried using @QuarkusComponentTest but the RestClient couldn't be found. I supposed it was because the builder wasn't available in the context, but this isn't Spring, and I couldn't find a way to make it available (maybe via a QuarkusTestResourceLifecycleManager?).

@QuarkusComponentTest
public class MyApiTest {
    @Inject
    @RestClient
    MyApi myApi;

    // ... execute tests
}

This gets me the following error:

java.lang.IllegalArgumentException: Annotation is not a registered qualifier: interface .eclipse.microprofile.rest.client.inject.RestClient

If I remove the @RestClient annotation, I get this error instead:

java.lang.IllegalStateException: The injected field [.wolfpack.den.doctolib.api.rest.DoctolibApi .wolfpack.den.doctolib.api.rest.DoctolibApiClientTest.api] expects a real component; but no matching component was registered

(I can't provide a stack trace on the computer I'm using right now, because it cannot access the live service I haven't been able to mock, hence another argument in favour of isolated unit tests.)

Another idea I tinkered about was to call the RestClientBuilder myself, but after a long series of trials and errors, I'm drawing the conclusion that it's impossible/unreasonable to do so outside of Quarkus's ArC container.

I'm using Quarkus's REST Client and I'd like to test that it serializes my queries with a correct format and parses the responses correctly. My idea is to use WireMock and test my client specifically, but I'd like to avoid @QuarkusTest. There are extensions in my application I don't wish to start because I haven't found how to mock them yet and I want my unit tests isolated:

  • I want my tests ran offline.
  • I specifically don't want to have to configure a live token to a live service in order to run my unit tests.
  • Ideally, when I'm testing just my REST client, I'd like to be able to do it outside of a Quarkus application.

I tried using @QuarkusComponentTest but the RestClient couldn't be found. I supposed it was because the builder wasn't available in the context, but this isn't Spring, and I couldn't find a way to make it available (maybe via a QuarkusTestResourceLifecycleManager?).

@QuarkusComponentTest
public class MyApiTest {
    @Inject
    @RestClient
    MyApi myApi;

    // ... execute tests
}

This gets me the following error:

java.lang.IllegalArgumentException: Annotation is not a registered qualifier: interface .eclipse.microprofile.rest.client.inject.RestClient

If I remove the @RestClient annotation, I get this error instead:

java.lang.IllegalStateException: The injected field [.wolfpack.den.doctolib.api.rest.DoctolibApi .wolfpack.den.doctolib.api.rest.DoctolibApiClientTest.api] expects a real component; but no matching component was registered

(I can't provide a stack trace on the computer I'm using right now, because it cannot access the live service I haven't been able to mock, hence another argument in favour of isolated unit tests.)

Another idea I tinkered about was to call the RestClientBuilder myself, but after a long series of trials and errors, I'm drawing the conclusion that it's impossible/unreasonable to do so outside of Quarkus's ArC container.

Share Improve this question edited Mar 20 at 7:51 Chop asked Mar 20 at 7:35 ChopChop 4,5695 gold badges30 silver badges60 bronze badges 3
  • For transparency, I've also asked another question, in order to see if I can mock the parts of the application/the extension that's causing me most issues when trying to run @QuarkusTest. – Chop Commented Mar 20 at 7:47
  • If your rest-client doesn't have request/response lifecycle components which are doing some I/O data manipulation I think you have to test the server side of MyApi. Instead, I was using wiremock to test rest-client request/response lifecycle components. – Luca Basso Ricci Commented Mar 20 at 8:11
  • I'm not sure I understand your comment, @LucaBassoRicci . It's a Quarkus Rest Client: MyApi is a JAX-RS-annotated interface, the Rest Client generates the I/O behaviour, which is precisely what I intend to test here: are my parameters formatted the way they should be, is the answer properly parsed. I'm not the server, I'm the client, hence I have no server side to test. The server side is my WireMock. Because the behaviour is generated, I'm having trouble unit-testing it. This is actually the core of this question. – Chop Commented Mar 20 at 8:45
Add a comment  | 

1 Answer 1

Reset to default 2

Well, you can't use a @QuarkusComponentTest to test the generated rest client, i.e. in this particular case the component implementation (a class that implements MyApi) is provided by the quarkus-rest-client extension. You can mock the MyApi injected in other components but that's a completely different task and would not help with your use case.

Note that in the @QuarkusComponentTest only the CDI container and MP Config is available. No other Quarkus extensions are used.

I don't know how the RestClientBuilder works outside a Quarkus app though.

发布评论

评论列表(0)

  1. 暂无评论