title: Excluding Configuration in Spring boot test
body: <p>I have the following setup in a maven project.
  Configuration class for the productive code:</p>
  
  <pre><code>package com.example;
  @Configuration
  public class MyConfiguration {
      @Bean
      public A beanA() {
        return new A();
      }
      ...
  }
  </code></pre>
  
  <p>Then I have one test, that has an internal <code>Configuration</code>:</p>
  
  <pre><code>package com.example;
  @RunWith(SpringRunner.class)
  @ContextConfiguration(classes = {MyConfiguration.class, SpringConfigurationTest.MyTestConfiguration.class})
  public class SpringConfigurationTest {
      @TestConfiguration
      static class MyTestConfiguration {
          @Bean
          @Primary
          public A beanA() {
            return mock(A.class);
          }    
      }
  }
  </code></pre>
  
  <p>The tests in this class work fine. Then I do have another Test class in sub package:</p>
  
  <pre><code>package com.example.sub;
  @RunWith(SpringRunner.class)
  @ContextConfiguration(classes = {MyConfiguration.class, AnotherSpringConfigurationTest.MyTestConfiguration.class})
  public class AnotherSpringConfigurationTest {
      @TestConfiguration
      static class MyTestConfiguration {
          @Bean
          public B beanB() {
            return new B()
          }    
      }
  }
  </code></pre>
  
  <p>When running tests in this class the test configuration from <code>SpringConfigurationTest.MyTestConfiguration</code> is also included. My guess the reason for this is the inclusion of <code>MyConfiguration</code> which lies in the root directory. The effect is that in <code>AnotherSpringConfigurationTest</code> the bean <code>A</code> is mocked instead of a real instance.</p>
  
  <p>How can I avoid that configuration classes inside other tests are 'leaked' into other tests?</p>
  
  <p>I have seen <a href="https://stackoverflow.com/questions/39690094/spring-boot-default-profile-for-integration-tests">Spring-boot default profile for integration tests</a> which uses Spring profiles in Spring Boot 1.4.1, while I am using Spring Boot 2.0.1. Though I am sure it could be done. </p>
  
  <p>Another approach I could think of would be to use component scanning to pick up the contexts and exclude all those that I do not want, but that is a bit cumbersome, as I would much prefer an approach where I define what I want to use instead of what should not be used.</p>
  
  <p>Is there an easy and elegant solution with Spring Boot 2 to avoid conflicting context configurations?</p>