Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Behavior changed of MockBean in 2.2.7 #21379

Closed
JBertaux opened this issue May 10, 2020 · 6 comments
Closed

Behavior changed of MockBean in 2.2.7 #21379

JBertaux opened this issue May 10, 2020 · 6 comments
Labels
for: external-project For an external project and not something we can fix

Comments

@JBertaux
Copy link

JBertaux commented May 10, 2020

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

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 10, 2020
@wilkinsona
Copy link
Member

wilkinsona commented May 10, 2020

Thanks for the sample.

The change in behaviour is a side-effect of the fix for #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.

@wilkinsona wilkinsona added the for: team-attention An issue we'd like other members of the team to review label May 10, 2020
@philwebb
Copy link
Member

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.

@philwebb
Copy link
Member

I've raise spring-cloud/spring-cloud-openfeign#337

@wilkinsona
Copy link
Member

Thanks, @philwebb. I should have remembered that, particularly as it was me that added it

@philwebb
Copy link
Member

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.

@philwebb philwebb added for: external-project For an external project and not something we can fix and removed for: team-attention An issue we'd like other members of the team to review status: waiting-for-triage An issue we've not yet triaged labels May 13, 2020
@JBertaux
Copy link
Author

Ok thank for the support :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project For an external project and not something we can fix
Projects
None yet
Development

No branches or pull requests

4 participants