Quarkus 入门单元测试描述了如何模拟注入的服务。但是,当尝试将其应用于注入的休息客户端时,这似乎不起作用。
在我的应用程序中,要注入的类属性是这样定义的
@Inject
@RestClient
MyService myService;
Run Code Online (Sandbox Code Playgroud)
在我的测试代码中,我创建了一个这样的模拟服务:
@Alternative()
@Priority(1)
@ApplicationScoped
public class MockMyService extends MyService {
@Override
public MyObject myServicemethos() {
return new MyObject();
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,此服务未注册或注释为 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)
...
Run Code Online (Sandbox Code Playgroud)
我可能可以通过添加一个额外的服务层来克服这个问题。但这感觉就像朝着错误的方向前进。
我该如何解决这个问题。
亲切的问候,
误会
小智 12
我刚刚遇到了同样的问题。文档中似乎有更新以及我遇到的一些极端情况,但谷歌搜索首先将我发送到这里,所以我将为未来的读者添加我的调查结果。
根据文档,您已经不需要创建模拟类 https://quarkus.io/guides/getting-started-testing#using-injectmock-with-restclient
但可以像这样使用它
服务等级
@RegisterRestClient(configKey = "country-api")
@ApplicationScoped
public interface MyService
Run Code Online (Sandbox Code Playgroud)
服务使用情况
@Inject
@RestClient
MyService myService;
Run Code Online (Sandbox Code Playgroud)
在测试中模拟它就像
@InjectMock
@RestClient
MyService myService;
Run Code Online (Sandbox Code Playgroud)
到目前为止一切顺利,但如果您需要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 # /
Run Code Online (Sandbox Code Playgroud)
在你的配置文件中。然后就会遇到这些问题
能够在 MicroProfile RestClient 上使用 InjectMock # 8622
在 MicroProfile RestClient 上使用 InjectMock # 9630
也就是说:如果您将 configKey 与 RegisterRestClient 一起使用,则必须注意“不要”
country-api/mp-rest/scope=javax.inject.Singleton #
Run Code Online (Sandbox Code Playgroud)
在配置文件中,该文件优先于 MyService 接口上的 @ApplicationScoped
您不需要另一个间接级别。
你可以简单地做:
@Alternative()
@Priority(1)
@ApplicationScoped
@RestClient
public class MockMyService extends MyService {
@Override
public MyObject myServicemethos() {
return new MyObject();
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,我添加了@RestClient注释。
更新
使用@RegisterRestClient而不是@RestClient@Tushar 在评论和回答中提到的可能更直观
更新 2
Quarkus 还内置了对 CDI bean 的模拟支持,请参阅https://quarkus.io/guides/getting-started-testing#further-simplification-with-injectmock和https://quarkus.io/blog/mocking/
| 归档时间: |
|
| 查看次数: |
4562 次 |
| 最近记录: |