Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quartz dynamic trigger not fire when using @SpringJUnitConfig, but it works fine with @SpringBootTest. Why is that? #1289

Open
ccmjga opened this issue Dec 24, 2024 · 0 comments

Comments

@ccmjga
Copy link

ccmjga commented Dec 24, 2024

Spring Boot 3.3.5
Spring Boot Stater Quartz 3.3.5 -> Quartz 2.3.2

@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@Testcontainers
@JooqTest
public class AbstractQuartzTest {
  public static PostgreSQLContainer<?> postgres =
      new PostgreSQLContainer<>("postgres:16.4-alpine").withDatabaseName("mjga");

  @DynamicPropertySource
  static void postgresProperties(DynamicPropertyRegistry registry) {
    registry.add("spring.datasource.url", postgres::getJdbcUrl);
    registry.add("spring.datasource.username", postgres::getUsername);
    registry.add("spring.datasource.password", postgres::getPassword);
    registry.add("spring.flyway.locations", () -> "classpath:db/migration/test");
  }

  static {
    postgres.start();
  }
}

  @Bean("emailJobSchedulerFactory")
 public SchedulerFactoryBean emailJobSchedulerFactory(DataSource dataSource) {
    SchedulerFactoryBean schedulerFactory = new SchedulerFactoryBean();
    schedulerFactory.setSchedulerName("email-scheduler");
    schedulerFactory.setDataSource(dataSource);
    Properties props = getCommonProps();
    props.setProperty("org.quartz.threadPool.threadCount", "10");
    schedulerFactory.setQuartzProperties(props);
    return schedulerFactory;
  }

// When using @SpringJUnitConfig it doesn't work. but @SpringBootTest works fine! 
@SpringJUnitConfig(classes = QuartzConfig.class)
public class SendEmailJobTest extends AbstractQuartzTest {
  private Boolean executed;

  @Autowired
  @Qualifier("emailJobSchedulerFactory") private SchedulerFactoryBean emailJobSchedulerFactory;

  @Test
  public void emailJobScheduler_givenDynamicJobAndTrigger_shouldRunJobAtDesiredTime()
      throws Exception {
    Scheduler emailJobScheduler = emailJobSchedulerFactory.getScheduler();
    assertTrue(emailJobScheduler.isStarted());
    JobDataMap jobDataMap = new JobDataMap();
    jobDataMap.put("userEmail", "[email protected]");

    JobDetail jobDetail =
        newJob(EmailJob.class)
            .withIdentity("email-job", "customer-service")
            .usingJobData(jobDataMap)
            .build();

    Trigger trigger =
        newTrigger()
            .withIdentity("email-trigger", "customer-service")
            .startAt(futureDate(1, DateBuilder.IntervalUnit.SECOND))
            .build();

    emailJobScheduler
        .getListenerManager()
        .addJobListener(
            new JobListener() {
              @Override
              public String getName() {
                return "TestJobListener";
              }

              @Override
              public void jobToBeExecuted(JobExecutionContext context) {}

              @Override
              public void jobExecutionVetoed(JobExecutionContext context) {}

              @Override
              public void jobWasExecuted(
                  JobExecutionContext context, JobExecutionException jobException) {
                executed = Boolean.TRUE;
              }
            });
    emailJobScheduler.scheduleJob(jobDetail, trigger);
    assertTrue(emailJobScheduler.isStarted());
    await()
        .atMost(3, java.util.concurrent.TimeUnit.SECONDS)
        .untilAsserted(() -> assertThat(executed).isTrue());
  }
}

Error

becase jobWasExecuted never executed so executed flag is null.

Expecting actual not to be null
java.lang.AssertionError: 
Expecting actual not to be null
	at com.yy.xx.integration.quartz.SendEmailJobTest.lambda$emailJobScheduler_givenDynamicJobAndTrigger_shouldRunJobAtDesiredTime$0(SendEmailJobTest.java:71)
	at org.awaitility.core.AssertionCondition.lambda$new$0(AssertionCondition.java:53)
	at org.awaitility.core.ConditionAwaiter$ConditionPoller.call(ConditionAwaiter.java:248)
	at org.awaitility.core.ConditionAwaiter$ConditionPoller.call(ConditionAwaiter.java:235)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
	at java.base/java.lang.Thread.run(Thread.java:1583)

I don't want to use @SpringBootTest, which takes too much time to load the following environment, I want to use @SpringJUnitConfig(classes = QuartzConfig.class) to write unit tests.

@ccmjga ccmjga changed the title Quartz dynamic trigger does not fire when using @SpringJUnitConfig, but it works fine with @SpringBootTest. Why is that? Quartz dynamic trigger not fire when using @SpringJUnitConfig, but it works fine with @SpringBootTest. Why is that? Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant