Quarkus getting started unittest describes how to mock injected services. However when trying to apply this to an injected rest client this does not seem to work.
In my application the class attribute to be injected is defined like this
@Inject
@RestClient
MyService myService;
In my test code I created a mock service like this:
@Alternative()
@Priority(1)
@ApplicationScoped
public class MockMyService extends MyService {
@Override
public MyObject myServicemethos() {
return new MyObject();
}
}
Please note that this service is not registered or annotated as a RestClient. Running my unittests like this gives the following error:
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)
...
I can probably overcome this by adding an additional service layer. But that feels like heading in the wrong direction.
How can I solve this.
Kind regards,
misl