4

So I've a Scheduler class where I inject a Service that calls a method at timeout. Basically like this:

@Singleton
@Startup
public class Scheduler{

@Resource
public TimerService timerService;

@Inject
private SomeService someService;

@PostConstruct
void postConstruct() {
        TimerConfig config= new TimerConfig();
        config.setPersistent(false);
        timerService.createIntervalTimer(60000, 60000, config);
}

@Timeout
public void runsomeTask(Timer timer) {
    someService.doSomething();
}

or this:

@Singleton
public class OtherScheduler{

@Inject
private SomeService someService;

@Schedule(second = "0", minute = "*/15", hour = "0", dayOfWeek = "*", persistent = false)
public void runsomeTask(Timer timer) {
    someService.doSomething();
    }
}

Until now i have only tested scheduler classes like those on my local server. So my question is how can I write a UnitTest for this? I would really appreciate some code examples or a good explanation.

avandeursen
  • 8,458
  • 3
  • 41
  • 51
  • Welcome to StackOverflow. Take a look at https://stackoverflow.com/q/27067049/274350 for suggestions on using the Java 8 Clock class to test time-based code – Richard Neish Dec 20 '18 at 10:34

0 Answers0