How to mock rest clients when unittesting a Quarkus application? - quarkus

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

I just hit the same problem. There seems to have updates in the documentation and some corner cases that I faced, but google search sent me here first so I'll add my investigation results for future readers.
According to the documentation you already do not need creating mock class
https://quarkus.io/guides/getting-started-testing#using-injectmock-with-restclient
but can use it like
Service class
#RegisterRestClient(configKey = "country-api")
#ApplicationScoped
public interface MyService
Service Usage
#Inject
#RestClient
MyService myService;
Mock it in the test like
#InjectMock
#RestClient
MyService myService;
So far so good but following the documentation https://quarkus.io/guides/rest-client if you need configKey you will probably finish with
# Your configuration properties
country-api/mp-rest/url=https://restcountries.eu/rest #
!!country-api/mp-rest/scope=javax.inject.Singleton # /
in your config file. And then will hit these issues
Ability to use InjectMock on MicroProfile RestClient # 8622
Usage of InjectMock on MicroProfile RestClient # 9630
Error when trying to Test RestClient # 12585
that say: if you are using configKey with RegisterRestClient have to care TO NOT HAVE
country-api/mp-rest/scope=javax.inject.Singleton #
in the config file which takes precedence before the #ApplicationScoped on the MyService interface

You don't need another level of indirection.
You can simply do:
#Alternative()
#Priority(1)
#ApplicationScoped
#RestClient
public class MockMyService extends MyService {
#Override
public MyObject myServicemethos() {
return new MyObject();
}
}
Note that I added the #RestClient annotation.
Update
It's probably more intuitive to use #RegisterRestClient instead of #RestClient as mentioned in the comments and in the answer by #Tushar
Update 2
Quarkus also has builtin mock support for CDI beans, see https://quarkus.io/guides/getting-started-testing#further-simplification-with-injectmock and https://quarkus.io/blog/mocking/

For quarkus release 1.1.0.Final (Latest as of writing this), use #RegisterRestClient
#Alternative()
#Priority(1)
#ApplicationScoped
#RegisterRestClient
public class MockMyService extends MyService {
#Override
public MyObject myServicemethos() {
return new MyObject();
}
}

Use #Mock annotation
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();
}
}

Related

How to use #Autowired annotation two or more different Component class for same service?

For example, have a class like as follows.
First XService service in class A is not null but second XService service in AmountValidator is null.I get NullPointerException I try to create bean new it works and then I get same exception when call AmountValidateService outsideRestService in XService.
How can I use XService everywhere that I use #Autowired annotation.
My main class:
#Service
class A extends AbstractA implements IA {
#Autowired
XService service; //first autowired definition. code go to check() method. service not null now.
public doSometing(){
validator.check();
service.methodA();
super.AbstractMethod();
}
}
Validator class used in class A :
class Validator<T> implements IValidator<T> {
public void check(){
rule.check(); // rule have a implements IValidator eg: amountValidator, dateValidator class
}
}
AmountValidator added to rule in class Validator.
#Component
class AmountValidator implements IValidator<T>{
#Autowired
XService service; // code comes here service is null. same service class mentioned above class A.
#Override
public void check(){
service.validateAmount(); // nullPointerException.
}
}
My main Service
#Component
class XService {
#Autowired
AmountValidateService outsideRestService;
public validateAmount(){
outsideRestService.validate(); // nullPointer when create XService with the `New` keyword
}
}
You have an error cause you are trying to create components/beans/services yourself. As i mentioned in comment when you create components yourself it - #Autowired doesn't work - thats you've got NPE
All classes annotated with #Component, #Service are considered special classes which are instantiated by Spring automatically via DI, instantiating them with new defeats the purpose of DI.
These special classes are named Spring Beans.
Every time the application starts, the framework instances all Spring Beans, and all #Autowired fields are injected by Spring automatically. But the Spring Beans must be defined somewhere in the class path. Else you will receive a NoSuchBeanDefinitionException
As an attempt to answer the question, since I don't have a stack trace nor all the Spring Bean definitions:
When you instantiate XService using new XService() your new instance will not actually initialize the field AmountValidateService outsideRestService, effectively leaving it as null.
You may set the field yourself but as I mentioned earlier, it defeats the purpose of DI
Your question is not complex, it is incomplete.

Spring boot #Inject proxy resolves to null

I'm refactoring an existing application to use Spring Boot. The issues I've faced here are generally of the type "why is this not working anymore".
I have three packages
- nl.myproject.boot
- nl.myproject
- nl.myproject.rest
My current problem is that all #Services that I #Inject in a #RESTController resolve to null when a method is called on them.
The service and dao are part of the nl.myproject package and the reason it's not nl.myproject.core is a legacy issue.
A related issue is that my #Configuration components don't seem to be loaded through #ComponentScan and I have to import them manually. I also had to exclude Test configuration to prevent Test configs from being loaded, which also seemed weird.
Internal calls from the service layer during start up, such as data preparation works normally. Any such manager is also #Injected. This is just to say that any of the typical injection mistakes such as manual instantiation or injecting a class instead of an interface don't apply.
I'd also be grateful for debugging tips. My Java has gotten a little rusty.
#EnableAutoConfiguration
#ComponentScan(basePackages= {
"nl.myproject",
"nl.myproject.boot",
"nl.myproject.dao",
"nl.myproject.service",
"nl.myproject.webapp"},
excludeFilters= {
#ComponentScan.Filter(type=FilterType.REGEX,pattern={".*Test.*"}),
#ComponentScan.Filter(type=FilterType.REGEX,pattern={".*AppConfig"})
}
)
#Configuration
#EnableConfigurationProperties
#Import({
JPAConfig.class,
RestConfig.class,
BootConfig.class
})
public class Startup {
public static void main(String[] args) throws Exception {
SpringApplication.run(Startup.class, args);
}
}
#RestController
#RequestMapping(value="/json/tags")
public class JsonTagController extends JsonBaseController {
#Inject
TagManager tagMgr;
public interface TagManager extends BaseManager<Tag,Long> {
[...]
}
#Service("tagManager")
public class TagManagerImpl extends BaseManagerImpl<Tag, Long> implements
TagManager {
#Inject
TagDao dao;
[...]
#Inject is a annotation specified by JSR-330 (standard) whereas #Autowired is annotation specified by Spring.
They just do the same dependency injection. You can both of them in the same code.
Just the modification (separation of the concerns) you need :
public interface TagManager {
[...]
}
#Service
public class TagManagerImpl implements TagManager {
#Inject
private TagDao dao;
// inject that service rather than extending
#Inject
private BaseManager<Tag,Long> baseManager;
}
public interface BaseManager<Tag,Long> {
[...]
}
#Service
public class BaseManagerImpl<Tag,Long> implements BaseManager<Tag,Long> {
....
}
Just one thing you do for checking, just modify to basePackages= {"nl.myproject"} - just provide only base package, that's enough for spring to scan the components in every package.
Hope this may help :)

Spring bean management using #Autowired

I have a messaging call that will process my payload which starts from MyClass. In load test i see that the first payload is getting over written by the next. All my classes are spring managed by #Autowired. Obviously the bean scope is singleton and thats why this is happening. But i do not want to use new operator and want it to be spring annotation configured. Is there any way to fix this issue of losing data ?
UPDATE
My configuration looks like below :
Public class MyClass {
...
#Autowired
public MyService myService;
...
}
#Component
#Scope(value="prototype", proxyMode=ScopedProxyMode.TARGET_CLASS)
public class MyService{
#Autowired
public Aone one;
#Autowired
public Atwo two;
...
}
#Component
#Scope(value="prototype")
public class Aone {
}
I am attempting this configuration after suggestions from net. For every call i get in MyClass a new instance of MyService will be created and from there on all other instances like Aone / Atwo should have new instance, will this configuration be ok ?

Spring inject test: Bean is not injected on test

I've created this custom test configuration:
#TestConfiguration
public static class RestTemplateTestConfiguration {
#Bean
#Primary
public static ApplicationDao applicationDao() {
ApplicationDao mock = Mockito.mock(ApplicationDao.class);
// some stuff code
return mock;
}
}
I've set a breakpoint on applicationDao, but it's never reached, and therefore mock is never injected.
EDIT
ApplicationDao is an #Repository annotated class:
#Repository
public interface ApplicationDao extends MongoRepository<Application, String> {
So, how could I override this #Repository annotated AplicationDao?
Currently, I'm getting this message when spring starts:
Skipping bean definition for [BeanMethod:name=applicationDao,declaringClass=net.gencat.transversal.espaidoc.functional.references.GroupReferencesTest$RestTemplateTestConfiguration]: a definition for bean 'applicationDao' already exists. This top-level bean definition is considered as an override.
Any ideas?
If your method applicationDao() is never called it means that your spring boot is not scanning the package where RestTemplateTestConfiguration is located.
The simplest solutions is to move the configuration under the same package (or its children) as the one that contains the class annotated with #SpringBootApplication.
OBS : This rule applies even though the configuration is in the test directory instead of main.
Another solution is to add #ComponentScan with the configuration package or to use #Import(RestTemplateTestConfiguration.class) at your spring boot test level.
SUGGESTION:
For your problem you can use:
#Mock
ApplicationDao applicationDao;
and if you have another service that uses this one to use:
#InjectMock
MyService myService;

Load service for interface only where there is no other service

In my application I have interface with default implementation that is service:
public interface MessageGenerator {
Message getMessage();
}
#Service
public class PropertiesMessageGenerator implements MessageGenerator {
#Override
public Message getMessage() {
return doSmth();
}
}
This service is loaded by spring boots #ComponentScan and everything works fine until I've added new implementation with #Profile
#Service
#Profile("p1")
public class ProfileMessageGenerator implements MessageGenerator {
#Override
public Message getMessage() {
return doSmthWithProfile();
}
}
How can I stop loading into DI service PropertiesMessageGenerator when ProfileMessageGenerator is in context?
PS
I cannot use #Profile("default") on MultipleMessages, because I've got more profiles that I load at one time.
A bit late to the party, but might help future visitors:
I've got it working in Spring Boot 2.5.1 by configuring my service like this:
#Service
#ConditionalOnMissingBean(value = BaseService.class, ignored = DefaultService.class)
public class DefaultService implements BaseService {
...
}
Without the ignored field in the annotation, the condition will match DefaultService and thus not instantiate it. That behavior is a bit strange, but excluding itself from the condition makes it work. With this, I can create an additional implementation of BaseService that is just annotated with #Service and instead it will not instantiate DefaultService but the other implementation.
If you don't use component scanning you could define and pick the bean using #Qualifier
If you want to maintain using component scanning use the #Conditional annotation.
Create your own implementation of 'org.springframework.context.annotation.Condition.class' and pass this to the annotation.
This could read a property to define when it returns true which could be set when what you want is in the context.
#ConditionalOnMissingBean(MessageGenerator.class) can be used if you do not want to implement your own Condition.
Or just annotate ProfileMessageGenerator with #Primary.

Resources