Spring boot 2.1 bean override vs. Primary

后端 未结 4 841
名媛妹妹
名媛妹妹 2020-12-28 14:04

With Spring Boot 2.1 bean overriding is disabled by default, which is a good thing.

However I do have some tests where I replace beans with mocked instances using Mo

4条回答
  •  有刺的猬
    2020-12-28 14:23

    I make the testing beans available only in test profile, and allow overriding for just while testing, like this:

    @ActiveProfiles("test")
    @SpringBootTest(properties = {"spring.main.allow-bean-definition-overriding=true"})
    class FooBarApplicationTests {
    
      @Test
      void contextLoads() {}
    }
    
    

    The bean I am mocking in the test configuration:

    @Profile("test")
    @Configuration
    public class FooBarApplicationTestConfiguration {
      @Bean
      @Primary
      public SomeBean someBean() {
        return Mockito.mock(SomeBean.class);
      }
    }
    
    

提交回复
热议问题