Hello,
After upgrading from 2.2.6 to 2.2.7, the behavior of MockBean
has changed.
One of my tests is failing, I mock a Feign Client with @MockBean
and now with the new version I have two beans for the Feign Client in the context: the mock one (MyClient$MockitoMock$...
) and I assume the real one with a strange type (HardCodedTarget(type=MyClient, name=my-client, url=...)
).
If I give the full reference of my client in the name of the MockBean @MockBean(name = "com.MyClient")
, it works as before
Here is a small project with an example : https://gitlab.com/GuanacoBE/feignmockbean
Comment From: wilkinsona
Thanks for the sample.
The change in behaviour is a side-effect of the fix for https://github.com/spring-projects/spring-boot/issues/20665. Unfortunately, FeignClientFactoryBean
is a FactoryBean<Object>
. This means that no type information is available from the class's generic signature and to determine the type of bean produced by the factory bean, it has to be initialised. Such initialisation is to be avoided as it can cause problems such as the one reported and fixed in #20665.
Given that @FeignClient
can be used on any type, I don't think it's possible for FeignClientFactoryBean
to provide any more type information than it does at the moment. I also don't think it's possible for us to fix your problem without regressing the fix for #20665. We may have to live with documenting the need to use @MockBean(name = "…")
when you're mocking a bean produced by a FactoryBean
with no type information in its signature. That's unfortunate, particularly in this case where the use of the factory bean is a package-private implementation detail of @FeignClient
.
Flagging for team attention to see if we can think of anything better.
Comment From: philwebb
I think we're pretty much stuck on our side, but the Spring Cloud team might be able to help us. They could update FeignClientsRegistrar
so that it adds a FactoryBean.OBJECT_TYPE_ATTRIBUTE
. I think we already have code in MockitoPostProcessor
that uses that attribute if it is available.
Comment From: philwebb
I've raise https://github.com/spring-cloud/spring-cloud-openfeign/issues/337
Comment From: wilkinsona
Thanks, @philwebb. I should have remembered that, particularly as it was me that added it…
Comment From: philwebb
I'm afraid there's nothing we can change in Spring Boot that will fix this without breaking the other issue. We'll leave this one for Spring Cloud to fix.
Comment From: JBertaux
Ok thank for the support :)