title: Spring boot 2.1 bean override vs. Primary
body: <p>With <a href="https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.1-Release-Notes" rel="noreferrer">Spring Boot 2.1 bean overriding is disabled</a> by default, which is a good thing.</p>
  
  <p>However I do have some tests where I replace beans with mocked instances using Mockito. With the default setting Tests with such a configuration will fail due to bean overriding.</p>
  
  <p>The only way I found worked, was to enable bean overriding through application properties:</p>
  
  <pre><code>spring.main.allow-bean-definition-overriding=true
  </code></pre>
  
  <p>However I would really like to ensure minimal bean definition setup for my test configuration, which would be pointed out by spring with the overriding disabled.</p>
  
  <p>The beans that I am overriding are either</p>
  
  <ul>
  <li>Defined in another configuration that imported into my test configuration</li>
  <li>Auto-discovered bean by annotation scanning</li>
  </ul>
  
  <p>What I was thinking should work in the test configuration overriding the bean and slap a <code>@Primary</code> on it, as we are used to for data source configurations. This however has no effect and got me wondering: Is the <code>@Primary</code> and the disabled bean overriding contradictory?</p>
  
  <p>Some example:</p>
  
  <pre><code>package com.stackoverflow.foo;
  @Service
  public class AService {
  }
  
  package com.stackoverflow.foo;
  public class BService {
  }
  
  package com.stackoverflow.foo;
  @Configuration
  public BaseConfiguration {
      @Bean
      @Lazy
      public BService bService() {
          return new BService();
      }
  }
  
  package com.stackoverflow.bar;
  @Configuration
  @Import({BaseConfiguration.class})
  public class TestConfiguration {
      @Bean
      public BService bService() {
          return Mockito.mock(BService.class);
      }
  }
  </code></pre>