Search code examples
jakarta-eetimerejbschedule

How to prepare basic test for TimerService scheduler?


Actually my code works great, unfortunately I found some problems when trying to write integration and unit tests for TimerService without preparing Arquillian container. My code, simplified:

@Startup
@Singleton
public class GeneralDataExecutor {

    @Resource
    private TimerService timerService;

    private Timer timer;

    @PostConstruct
    public void init() {
            timer = timerService.createCalendarTimer(createScheduleExpression(), createTimerConfig());
        }
    }

    @Timeout
    public void execute() {
        //some code
    }
}

Any ideas/tips how to get things done?


Solution

  • There are frameworks which help to mock the ejb-container and help to handle the asynchronity.

    Look at the Example of a Timer-Bean

    @Stateless
    public class StatelessTimerEJB extends CountingBean {
    
       @PostConstruct
       public void postConstruct() {
           setPostConstructCalled();
       }
    
       @Timeout
       public void callAsynch()  {
           if (isPostConstructCalled()) {
               logcall();
               return;
           } else {
               logger.error("postconstruct did not work for this instance");
           }
       }
    }
    

    and how it is tested using ioc-unit Asynchronousmanager in

    TestAsynchronousManager