I would like to deactivate some functions at the runtime when the application is running in a Unit Test mode. Is there a way to recognize it?
Thanks for any tips!
I would like to deactivate some functions at the runtime when the application is running in a Unit Test mode. Is there a way to recognize it?
Thanks for any tips!
Thanks for tips!
This code works for my purposes:
public static boolean junitIsActive() {
for (StackTraceElement s : Thread.currentThread().getStackTrace()) {
if (s.getClassName().contains("org.junit.runners.model")) {
return true;
}
}
return false;
}
Thanks @vaio.
@Bean
boolean testMode() {
for (StackTraceElement s : Thread.currentThread().getStackTrace()) {
if (s.getClassName().contains("org.springframework.boot.test.context.SpringBootContextLoader")) {
return true;
}
}
return false;
}
In my case (Spring boot batch), above code will work.
I just want to make some Job
not run in test mode.
@Bean
public Job reindexLeadsJob(JobCompletionNotificationListener listener, Step stepReindexFromMongoDB) {
if (testMode) {
return null;
}
return jobBuilderFactory.get("reindexLeadsJob")
.incrementer(new RunIdIncrementer())
.listener(listener)
.flow(stepReindexFromMongoDB)
.end()
.build();
}
Now in test mode, I simple return null in the JobConfig
. Is there other way more elegant?