Search code examples
testingejbschedule

How to test an EJB @Schedule


I am using an EJB schedule to do email daemon task:

@Schedule(hour="8, 10, 12, 14, 16, 18")
    public void sendExpirationReminders() {
}

How can I test these methods on a running server?

I currently use HtmlUnit for most of my testing so I can make real browser requests. But I'm at a loss how to test the inner contents of these daemon methods.


Solution

  • The short answer is: you can't.

    What some people suggest (which I agree) is to separate the code that does the scheduling with the code that actually performs an operation. If you follow this, you'll have one class that has the @Schedule annotation, which only responsibility is to call the class that performs the operation.

    Why you can test this? It's easy, you can't mock Date (or the clock) in an application server.