单元测试 Quarkus 应用程序时如何模拟休息客户端?

How to mock rest clients when unittesting a Quarkus application?

Quarkus getting started unittest 描述了如何模拟注入的服务。但是,当尝试将其应用于注入的休息客户端时,这似乎不起作用。

在我的应用程序中,要注入的 class 属性定义如下

  @Inject
  @RestClient
  MyService myService;

在我的测试代码中,我创建了一个这样的模拟服务:

@Alternative()
@Priority(1)
@ApplicationScoped
public class MockMyService extends MyService {

    @Override
    public MyObject myServicemethos() {
        return new MyObject();
    }
}

请注意,此服务未注册或注释为 RestClient。 运行 我这样的单元测试给出了以下错误:

org.junit.jupiter.api.extension.TestInstantiationException: TestInstanceFactory [io.quarkus.test.junit.QuarkusTestExtension] failed to instantiate test class [...MyMediatorTest]: io.quarkus.builder.BuildException: Build failure: Build failed due to errors
    [error]: Build step io.quarkus.arc.deployment.ArcAnnotationProcessor#build threw an exception: javax.enterprise.inject.spi.DeploymentException: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type ...MyService and qualifiers [@RestClient]
    - java member: ...MyMediator#myService
    - declared on CLASS bean [types=[java.lang.Object, ...MyMediator], qualifiers=[@Default, @Any], target=...MyMediator]

    at org.junit.jupiter.engine.descriptor.ClassTestDescriptor.invokeTestInstanceFactory(ClassTestDescriptor.java:314)
  ...

我可能可以通过添加一个额外的服务层来克服这个问题。但这感觉像是在朝着错误的方向前进。

我该如何解决这个问题。

亲切的问候,

错误

您不需要另一个间接级别。

你可以简单地做:

@Alternative()
@Priority(1)
@ApplicationScoped
@RestClient
public class MockMyService extends MyService {

    @Override
    public MyObject myServicemethos() {
        return new MyObject();
    }
}

请注意,我添加了 @RestClient 注释。

更新

使用 @RegisterRestClient 而不是 @RestClient 可能更直观,如评论和@Tushar

的回答中所述

更新 2

Quarkus 还具有对 CDI bean 的内置模拟支持,请参阅 https://quarkus.io/guides/getting-started-testing#further-simplification-with-injectmock and https://quarkus.io/blog/mocking/

对于 quarkus 1.1 版。0.Final(撰写本文时的最新版本),使用@RegisterRestClient

@Alternative()
@Priority(1)
@ApplicationScoped
@RegisterRestClient
public class MockMyService extends MyService {
    @Override
    public MyObject myServicemethos() {
        return new MyObject();
    }
}

使用@Mock 注解

import io.quarkus.test.Mock;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import javax.enterprise.context.ApplicationScoped;

@Mock
@ApplicationScoped
@RestClient
public class MockMyService extends MyService {

    @Override
    public MyObject myServicemethos() {
        return new MyObject();
    }
}

我刚遇到同样的问题。文档和我遇到的一些极端情况似乎有更新,但 google 搜索首先将我发送到这里,所以我会为未来的读者添加我的调查结果。

根据文档,您已经不需要创建模拟 class https://quarkus.io/guides/getting-started-testing#using-injectmock-with-restclient

但可以像

一样使用它

服务class

@RegisterRestClient(configKey = "country-api")
@ApplicationScoped
public interface MyService

服务使用情况

@Inject
@RestClient
MyService myService;

在测试中模拟一下

@InjectMock
@RestClient 
MyService myService;

到目前为止一切顺利,但如果您需要 configKey,请遵循文档 https://quarkus.io/guides/rest-client,您可能会完成

# Your configuration properties
country-api/mp-rest/url=https://restcountries.eu/rest #
!!country-api/mp-rest/scope=javax.inject.Singleton # /

在你的配置文件中。然后会遇到这些问题

Ability to use InjectMock on MicroProfile RestClient # 8622

Usage of InjectMock on MicroProfile RestClient # 9630

Error when trying to Test RestClient # 12585

也就是说:如果您将 configKey 与 RegisterRestClient 一起使用,则必须注意 不要

country-api/mp-rest/scope=javax.inject.Singleton # 

在MyService接口上优先于@ApplicationScoped之前的配置文件