2

I'm testing a component which wraps a Quartz Scheduler object.

My problem is that Quartz is doing some asynchronous processing internally and I can't write in my test code something like this:

 Mockito.when(configurationMock.getId()).thenReturn(CONFIG_ID); target.addJob(configurationMock); Scheduler sched = (Scheduler) Whitebox.getInternalState(target, "scheduler"); assertTrue(sched.checkExists(new JobKey(configurationMock.getId()))); 

because it is possible that when I check the job's existence it is not there yet.

I checked the JUnit API but there is no assertWithTimeout() or something like that. Did I miss something?

1 Answer 1

2

I generally use a CountDownLatch - but it requires some form of callback method, something like:

CountDownLatch done = new CountDownLatch(1); target.onJobComplete(new Runnable() { public void run() { done.countdown(); }}); Scheduler sched = (Scheduler) Whitebox.getInternalState(target, "scheduler"); done.await(timeout); 

If you don't have a callback or a way to check if the task has been scheduled yet, you could simply wait:

target.addJob(configurationMock); Scheduler sched = (Scheduler) Whitebox.getInternalState(target, "scheduler"); //wait up to 1 second for (int i = 0; i < 100; i++) { if (!sched.checkExists(new JobKey(configurationMock.getId()))) Thread.sleep(10); else break; } assertTrue(sched.checkExists(new JobKey(configurationMock.getId()))); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.