简体   繁体   English

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

[英]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). 我有一个Java @Configuration类,其FOO @Bean带有@ConditionalOnBean(BAR.class).批注@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. 正如我期望的那样,这个bean是由导入程序项目提供或不提供的,因此它不在我的项目中的任何地方。

In my integration test, I mock Bar.class by means of @MockBeans . 在我的集成测试,我嘲笑Bar.class借助@MockBeans But for some reason Spring-Boot -debug tells me it did not found it so my conditional bean has not been loaded. 但是由于某种原因,Spring-Boot -debug告诉我它没有找到它,所以我的条件bean没有被加载。

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 我无法使它工作

PS> I discovered that manually re-registering the @Bean in the same @Configuration as the conditional does not see it neither! PS>我发现在与条件相同的@Configuration中手动重新注册@Bean都看不到! 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. 您需要非常注意添加Bean定义的顺序,因为这些条件是根据到目前为止已处理的内容来评估的。 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). 出于这个原因,我们建议仅在自动配置类上使用@ConditionalOnBean和@ConditionalOnMissingBean批注(因为保证在添加任何用户定义的bean定义之后可以加载它们)。

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

P.S3> I found out that the MockitoTestExecutionListener is executed after the OnBeanCondition class. P.S3>我发现MockitoTestExecutionListener是在OnBeanCondition类之后执行的。 This seems my problem totally. 这似乎完全是我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Boot @MockBeans - 如何在多个测试类中使用相同的 bean - Spring Boot @MockBeans - How to use the same beans in multiple test classes spring-boot-test spyBean注释不注入依赖项 - spring-boot-test spyBean annotation not injecting dependency Spring Boot ConditionalOnBean 注解 - Spring boot ConditionalOnBean annotation @BeforeAll JUnit/spring-boot-test 在应用程序上下文启动时运行的替代方案 - @BeforeAll JUnit/spring-boot-test alternative that runs when application context starts 尽管 Spring-boot-starter-parent 中存在 Spring-boot-test 依赖项,但必须手动导入它们 - 为什么? - Spring-boot-test dependencies having to be manually imported inspite of them being present in Spring-boot-starter-parent - Why? Spring Boot与@ConditionalOnBean的传递@Component依赖关系 - Spring Boot transitive @Component dependencies with @ConditionalOnBean Spring-boot中ConditionalOnBean的意外行为 - Unexpected behavior for ConditionalOnBean in Spring-boot Spring Boot 自动配置 @ConditionalOnBean 行为怪异 - Spring boot autoconfiguration @ConditionalOnBean behaving weirdly Spring @ConditionalOnBean 结合@Component - Spring @ConditionalOnBean in combination with @Component Java Spring Bean 依赖注入,conditionalOnBean - Java Spring Bean dependency injection, conditionalOnBean
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM