ses
ses

Reputation: 13362

Quarkus. Testing with spy in the integration test

I see that there is some limitation when it comes to mocking/spying in Quarkus.

The question is, how would I do something similar to this with the means that Quarkus provides? To spy on restClient and see how many times it was invoked.

    @QuarkusTest
    public class MyResourceTest {

      @Inject
      @RestClient
      MyRestClient restClient;

      @Ineject
      SomeServiceThatCallRestClient someService;

      @Test
      public void test() {
         restClientSpy = spy(restClient);

         someService.doSomething();     // it would call the restClient    

         assertThat(clientSpy.times(), is(equalTo(1)));

      }

Upvotes: 2

Views: 905

Answers (1)

Juliano Suman Curti
Juliano Suman Curti

Reputation: 414

What you could is to create an ApplicationScoped Beam as a wrapper for your restClient.

Example:

@ApplicationScoped
public class RestClientWrapper {

    @Inject
    @RestClient
    protected MyRestClient restClient;

    public String  getString(long stringId) {
        return restClient.getString(stringId);
    }
}

Then, your test would be:

@QuarkusTest
public class MyResourceTest {

  @InjectSpy
  protected RestClientWrapper restClient;

  @Inject
  protected SomeServiceThatCallRestClient someService;

  @Test
  public void test() {

     someService.doSomething();     // it would call the restClient    

     Mockito.verify(restClient).getString(anyLong());
     Mockito.verifyNoMoreInteractions(restClient);
  }

Upvotes: 0

Related Questions