In my application there are several tasks which have to get executed periodically in production but should not runx in my integration tests. (These classes are annotated with Spring's @Scheduled annotation.) I have two Spring Boot runner classes:
Application, defined as:
@SpringBootApplication
@EnableScheduling
@EnableAsync
public class Application {
public static void main(String[] args) throws UnknownHostException {
SpringApplication app = new SpringApplication(Application.class);
SimpleCommandLinePropertySource source = new SimpleCommandLinePropertySource(args);
...
}
and ApplicationTest, defined as:
@SpringBootApplication
@EnableAsync
public class ApplicationTest {
public static void main(String[] args) throws UnknownHostException {
SpringApplication.run(ApplicationTest.class, args);
}
}
My tests are annotated with the following annotations:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(ApplicationTest.class)
@WebIntegrationTest(randomPort = true)
I'm defining the class ApplicationTest as the @SpringApplicationConfiguration class, which should be used running the test. Unlike the Application class ApplicationTest is missing the required annotation to enable scheduling.
When I run the test using mvn test or even mvn verify, the annotation get's ignored, starting the application using the Application class and thus starting the scheduled tasks.
What am I missing?
ApplicationandApplicationTestin the same package by any chance? If so, classpath scanning... Having said that, I wouldn't do this but rather enable scheduling with a profile (you could enable the profile in the main method of yourApplicationclass to make that transparent.