Prevent Application / CommandLineRunner classes from executing during JUnit testing

前端 未结 7 1499
既然无缘
既然无缘 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:17

    I'm a bit late to the party, but a reasonable approach is to mark the bean with @ConditionalOnProperty, e.g.

    @ConditionalOnProperty(prefix = "job.autorun", name = "enabled", havingValue = "true", matchIfMissing = true)
    public CommandLineRunner myRunner() {...}
    

    The following annotation will then disable it in tests:

    @SpringBootTest(properties = {"job.autorun.enabled=false"})
    

提交回复
热议问题