title: Java Spring Boot Test: How to exclude java configuration class from test context
body: <p>I have a Java web app with spring boot</p>
  
  <p>When run test I need to exclude some Java config files:</p>
  
  <p>Test config (need to include when test run):</p>
  
  <pre><code>@TestConfiguration
  @PropertySource("classpath:otp-test.properties")
  public class TestOTPConfig { }
  </code></pre>
  
  <p>Production config (need to exclude when test run):</p>
  
  <pre><code> @Configuration
   @PropertySource("classpath:otp.properties")
   public class OTPConfig { }
  </code></pre>
  
  <p>Test class (with explicit config class):</p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @SpringBootTest(classes = TestAMCApplicationConfig.class)
  public class AuthUserServiceTest { .... }
  </code></pre>
  
  <p>Test config: </p>
  
  <pre><code>@TestConfiguration
  @Import({ TestDataSourceConfig.class, TestMailConfiguration.class, TestOTPConfig.class })
  @TestPropertySource("classpath:amc-test.properties")
  public class TestAMCApplicationConfig extends AMCApplicationConfig { }
  </code></pre>
  
  <p>Also have class:</p>
  
  <pre><code>@SpringBootApplication
  public class AMCApplication { }
  </code></pre>
  
  <p>When test is running <code>OTPConfig</code> used, but I need <code>TestOTPConfig</code>...</p>
  
  <p><strong>How can I do it?</strong></p>