如何重用我的配置并在 Spring Boot 测试中排除另一个配置?

How to reuse my configurations and exclude another one in SpringBoot Test?

如何重用我的主要配置但排除一些..?

喜欢:

@SpringBootApplication
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class, 
                ConfigOne.class,
                ConfigTwo.class,
                ConfigThree.class //I want to exclude the ConfigThree in my tests and use my own ConfigThreeTest.class
                ).run(args);
    }
}

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class MyTests {

    //test...
}

@Configuration
public class ConfigThreeTest {

    //config...

}

在上面的示例中,我想排除 ConfigThree.class 并使用 ConfigThreeTest.class

试试这个

@SpringBootApplication(exclude = {demo.class})

在您的 MyTests class 中,您可以指定要用于配置的 class(es)。

@SpringBootTest(classes = {ConfigOne.class, ConfigTwo.class},webEnvironment = WebEnvironment.RANDOM_PORT)