Whimusical
Whimusical

Reputation: 6649

Are Spring-Boot-Test @MockBeans supposed to qualify for @ConditionalOnBean conditions?

I have a java @Configuration class with a FOO @Bean annotated with @ConditionalOnBean(BAR.class). As I expect this bean to be or not provided by the importer project, it's not present anywhere in my project.

In my integration test, I mock Bar.class by means of @MockBeans. But for some reason Spring-Boot -debug tells me it did not found it so my conditional bean has not been loaded.

I'm almost sure this situation has worked properly in the past, but did I configure anything extra? I can't manage to make it work

P.S> I discovered that manually re-registering the @Bean in the same @Configuration as the conditional does not see it neither! Is there any known bug related to his?

Autoreply: The culprit in this case is

You need to be very careful about the order that bean definitions are added as these conditions are evaluated based on what has been processed so far. For this reason, we recommend only using @ConditionalOnBean and @ConditionalOnMissingBean annotations on auto-configuration classes (since these are guaranteed to load after any user-defined beans definitions have been added).

P.S2> I realized Bar.class is an interface but I don't see why shouldn't it work as long as an implementation is present

P.S3> I found out that the MockitoTestExecutionListener is executed after the OnBeanCondition class. This seems my problem totally.

Upvotes: 5

Views: 2898

Answers (1)

Related Questions