You can extract @EnableScheduling
to a separate configuration class like:
@Configuration
@Profile("!test")
@EnableScheduling
class SchedulingConfiguration {
}
Once it's done, the only thing that is left is to activate "test" profile in your tests by annotating test classes with:
@ActiveProfiles("test")
Possible drawback of this solution is that you make your production code aware of tests.
Alternatively you can play with properties and instead of annotating SchedulingConfiguration
with @Profile
, you can make it @ConditionalOnProperty
with property present only in production application.properties
. For example:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Configuration
@ConditionalOnProperty(value = "scheduling.enabled", havingValue = "true", matchIfMissing = true)
@EnableScheduling
static class SchedulingConfiguration {
}
}
Scheduler will not run in tests when you do one of following:
add property to src/test/resources/application.properties
:
scheduling.enabled=false
customize @SpringBootTest
:
@SpringBootTest(properties = "scheduling.enabled=false")