Search code examples
javaspring-bootjunitquartz-schedulereventtrigger

How to test proper working of Quartz job in Spring Boot application


I want to test if my Quartz trigger is working as it supposes in practice. My Quartz configuration looks like:

@Configuration
public class QuartzConfiguration {

  @Bean
  public JobDetail verificationTokenRemoverJobDetails() {
    return
        JobBuilder
            .newJob(VerificationTokenQuartzRemoverJob.class)
            .withIdentity("Job for verification token remover")
            .storeDurably()
            .build();
  }

  @Bean
  public Trigger verificationTokenRemoverJobTrigger(JobDetail jobADetails) {
    return
        TriggerBuilder
            .newTrigger()
            .forJob(jobADetails)
            .withIdentity("Trigger for verification token remover")
            .withSchedule(CronScheduleBuilder.cronSchedule("0 0 0/2 1/1 * ? *"))
            .build();
  }
}

and my Job class looks like:

@AllArgsConstructor
public class VerificationTokenQuartzRemoverJob implements Job {

  private VerificationTokenRepository verificationTokenRepository;

  @Override
  public void execute(JobExecutionContext context) {
    verificationTokenRepository.deleteAllByCreatedLessThan(LocalDateTime.now().minusMinutes(30));
  }
}

When I am starting my Spring Boot application in logs I can realize that Job is working and triggered cyclical but it's not enough to confirm the proper working.

That's why I decided to create a JUnit test. I found a tutorial: click but an owner used a clause while(true) which according to this topic: click is not a preferable option. Here occurs a question, is there any other option to verify the Job class name, the identity of the trigger and check if CRON expression and the concrete job are called as often as possible?

If it possible I will be grateful for suggestions on how to reach a desirable effect.


Solution

  • With SpringBoot, You could do easier doing the following

    --- Option 1 ---

    @Configuration
    // This is important!
    @EnableScheduling
    public class Configuration{
     // TODO Change to 0 0 0/2 1/1 * ? *
     @Scheduled(cron = "0 15 10 15 * ?")
     public void scheduleTaskUsingCronExpression() {
    
        long now = System.currentTimeMillis() / 1000;
         System.out.println(
            "schedule tasks using cron jobs - " + now);
      }      
    

    }

    Full Example: https://www.baeldung.com/spring-scheduled-tasks

    --- Option 2-> Programatically ---

    @Configuration
    @EnableScheduling
    
    public class Configuration implements SchedulingConfigurer {
       @Bean(destroyMethod = "shutdown")
       public Executor taskExecutor() {
          return Executors.newScheduledThreadPool(100);
       }
    
        @Override
                public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
                CronTrigger cronTrigger
                = new CronTrigger("* * * * * *");
    
                        taskRegistrar.setScheduler(taskExecutor());
                        taskRegistrar.addTriggerTask(
                                new Runnable() {
                                    @Override public void run() {
                                        System.out.println("RUN!!!");
                                    }
                                },
                                cronTrigger
                        );
                    }
    

    }