Overriding beans in Integration tests

后端 未结 7 1894
眼角桃花
眼角桃花 2020-12-23 09:16

For my Spring-Boot app I provide a RestTemplate though a @Configuration file so I can add sensible defaults(ex Timeouts). For my integration tests I would like to mock the R

7条回答
  •  悲&欢浪女
    2020-12-23 09:48

    With @Primary annotation, Bean overriding works with Spring Boot 1.5.X but fails with Spring Boot 2.1.X it throw error:

    Invalid bean definition with name 'testBean' defined in sample..ConfigTest$SpringConfig:.. 
    There is already .. defined in class path resource [TestConfig.class]] bound
    

    Please add below properties= which will instruct Spring explicitly to allow overriding, it is self explainatory.

    @SpringBootTest(properties = ["spring.main.allow-bean-definition-overriding=true"])
    

提交回复
热议问题