How to exclude *AutoConfiguration classes in Spring Boot JUnit tests?

前端 未结 13 1011
名媛妹妹
名媛妹妹 2020-12-01 01:51

I tried:

@RunWith(SpringJUnit4ClassRunner.class)
@EnableAutoConfiguration(exclude=CrshAutoConfiguration.class)
@SpringApplicationConfiguration(classes = Appl         


        
相关标签:
13条回答
  • 2020-12-01 02:36

    With the new @SpringBootTest annotation, I took this answer and modified it to use profiles with a @SpringBootApplication configuration class. The @Profile annotation is necessary so that this class is only picked up during the specific integration tests that need this, as other test configurations do different component scanning.

    Here is the configuration class:

    @Profile("specific-profile")
    @SpringBootApplication(scanBasePackages={"com.myco.package1", "com.myco.package2"})
    public class SpecificTestConfig {
    
    }
    

    Then, the test class references this configuration class:

    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = { SpecificTestConfig.class })
    @ActiveProfiles({"specific-profile"})
    public class MyTest {
    
    }
    
    0 讨论(0)
提交回复
热议问题