Prevent Application / CommandLineRunner classes from executing during JUnit testing

前端 未结 7 1480
既然无缘
既然无缘 2020-12-07 22:47

If in your TestCase class there is this annotations:

@SpringApplicationConfiguration(classes = {Application.class})

this will cause the

相关标签:
7条回答
  • 2020-12-07 23:20

    Previous answers didn't work wor me. I ended up using different profiles - example for the init method in Spring Boot:

    SpringApplication app = new SpringApplication(AppConfig.class);
    app.setAdditionalProfiles("production");
    app.run(args);
    

    This is not executed during the tests so we're safe here.

    All tests have their own profile "test" (which is useful in many other ways, too):

    @RunWith(SpringJUnit4ClassRunner.class)
    @ActiveProfiles("test")
    public class MyFancyTest {}
    

    The command-line runner is annotated with the "production" profile so the tests ignore it:

    @Component
    @Profile("production")
    public class JobCommandLineRunner implements CommandLineRunner {}
    
    0 讨论(0)
提交回复
热议问题