Spring-Boot-Test @MockBeans 是否应该符合@ConditionalOnBean 条件?

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

我有一个 java @Configuration class 和一个 FOO @Bean 注释 @ConditionalOnBean(BAR.class). 因为我希望进口商提供或不提供这个 bean项目,它不存在于我项目的任何地方。

在我的集成测试中,我通过 @MockBeans 模拟 Bar.class。但是由于某种原因 Spring-Boot -debug 告诉我它没有找到它所以我的条件 bean 还没有被加载。

我几乎可以肯定这种情况在过去是正常的,但我是否配置了任何额外的东西?我无法让它工作

P.S> 我发现在与条件相同的@Configuration 中手动重新注册@Bean 也看不到它!是否有与他相关的已知错误?

自动回复:这种情况下的罪魁祸首是

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> 我意识到 Bar.class 是一个接口,但我不明白为什么只要存在实现它就不能工作

P.S3> 我发现 MockitoTestExecutionListener 在 OnBeanCondition class 之后执行。这似乎完全是我的问题。

这个和这个解释了为什么不可能: