Unit test of a spring scheduler configuration using Mockito.spy


here is a little sample on how to write scheduled monitoring unit test using spring and mockito spy factory method.

In the legacy code there is a job that is defined to purge the database each 8 hour using a spring defined quarrtz job:


<beans xmlns:xsi="" 
   <bean class=""


 <beans xmlns:xsi="" xmlns="" xsi:schemalocation="  
   <bean class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean" id="scheduledDatabasePurgeJob">  
     <property name="targetObject" ref="scheduledDatabasePurgeMonitor">  
     <property name="targetMethod" value="doIt">  
     <property name="concurrent" value="false">  
   <bean class="org.springframework.scheduling.quartz.SimpleTriggerBean" id="databasePurgeTrigger">  
     <!-- see the example of method invoking job above -->  
     <property name="jobDetail" ref="scheduledDatabasePurgeJob">  
     <!-- first start after 1 milliseconds -->  
     <property name="startDelay" value="1">  
     <!-- repeat every (schedule.databasePurge.repeatIntervalMs) milliseconds / default 8 hours -->  
     <property name="repeatInterval" value="${schedule.databasePurge.repeatIntervalMs:28800000}">  
   <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
     <property name="triggers">  
         <ref bean="databasePurgeTrigger">  
   <!-- official howto :  

here is the sample purgemonitor class:

  * ScheduledDatabasePurgeMonitor  
 public class ScheduledDatabasePurgeMonitor {  
   private final static Logger logger = LoggerFactory.getLogger(ScheduledDatabasePurgeMonitor.class.getName());  
   public void doIt() {  
     logger.debug("database purge right now");  
   void purgeDatabase() {  
     logger.debug("* purge database *");  

we would like to ensure in an unit test that the spring configuration enable the purge scheduling each N ms :

  • we would like a little N to avoid an 8 hour unit test
  • we would like to mock the purge service

So we need a sping spy'ed bean and here is a way to do this :
Unit test class :

 import org.junit.Assert;  
 import org.junit.Before;  
 import org.junit.Ignore;  
 import org.junit.Test;  
 import org.junit.runner.RunWith;  
 import org.slf4j.Logger;  
 import org.slf4j.LoggerFactory;  
 import org.springframework.beans.factory.annotation.Autowired;  
 import org.springframework.test.context.ContextConfiguration;  
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
 import static org.mockito.Mockito.spy;  
 import static org.mockito.Mockito.times;  
 import static org.mockito.Mockito.verify;  
  * ScheduledDatabasePurgeMonitorTest IT Test of the database purge monitoring  
 public class ScheduledDatabasePurgeMonitorTest {  
      private final static Logger logger = LoggerFactory.getLogger(ScheduledDatabasePurgeMonitorTest.class.getName());  
   ScheduledDatabasePurgeMonitor scheduledDatabasePurgeMonitor;  
   private Integer purgeRepeatIntervalMs;
   public void setup() {
       // avoid the database purge here
       * GIVEN ScheduledDatabasePurgeMonitor implementation WHEN scheduler  
       * trigger THEN purge service is called  
      public void database_purge_monitor_should_be_periodically_invoked() {  
     // THEN"verify first call of purgeDatabase");  
     int callNumber = 1;  
     verify(scheduledDatabasePurgeMonitor, times(callNumber)).purgeDatabase();  
     for (int i=0; i<3 ; i++) {  
       waitForNextScheduledUpdate();callNumber++;"verify next call ({}) of purgeDatabase", callNumber);  
       verify(scheduledDatabasePurgeMonitor, times(callNumber)).purgeDatabase();  
   private void waitForNextScheduledUpdate() {  
     try {  
       long waitIntMs = getPurgeRepeatIntervalMs() + 100;  
       logger.debug("wait for {} seconds", (waitIntMs / 1000));  
     } catch (InterruptedException e) {"InterruptedException oO");  
   public ScheduledDatabasePurgeMonitor getScheduledDatabasePurgeMonitor() {  
     return scheduledDatabasePurgeMonitor;  
   public void setScheduledDatabasePurgeMonitor(ScheduledDatabasePurgeMonitor scheduledDatabasePurgeMonitor) {  
     this.scheduledDatabasePurgeMonitor = scheduledDatabasePurgeMonitor;  
   public Integer getPurgeRepeatIntervalMs() {  
     return purgeRepeatIntervalMs;  
   public void setPurgeRepeatIntervalMs(Integer purgeRepeatIntervalMs) {  
     this.purgeRepeatIntervalMs = purgeRepeatIntervalMs;  

Unit test spring context :

 <?xml version="1.0" encoding="UTF-8"?>  
 <beans xmlns=""  
   <import resource="classpath:/META-INF/spring/jasypt-context.xml"/>  
   <bean class="" depends-on="jasyptPrerequisites">  
     <constructor-arg ref="configurationEncryptor"/>  
     <property name="locations">  
   <bean id="toSpyScheduledDatabasePurgeMonitor"  
   <bean id="scheduledDatabasePurgeMonitor"  
      class="org.mockito.Mockito" factory-method="spy">  
     <constructor-arg ref="toSpyScheduledDatabasePurgeMonitor" />  
   <import resource="classpath:META-INF/spring/services-scheduled-trigger-context.xml"/>  
   <bean id="purgeRepeatIntervalMs" class="java.lang.Integer">  
     <constructor-arg value="${schedule.databasePurge.repeatIntervalMs}"/>  

unit test properties file :

# test purpose : ask iaas quota each 2 seconds

you would notice that Mockito.spy(object) static method is used into the spring unit test context :

   <bean id="scheduledDatabasePurgeMonitor"  
      class="org.mockito.Mockito" factory-method="spy">  
     <constructor-arg ref="toSpyScheduledDatabasePurgeMonitor" />  

Aucun commentaire:

Enregistrer un commentaire

Mots clés du blog

10.1 4G acceptancetest adb androï Android androïd Android7 api appender appengine application applications archive array assistantematernelle astuce auth0 authentication authority automation Axis bash bearer blog boot bootloader bower build bundle c calendrier camille combal cdi certificate cf client cloudfoundry collaboratif command commandes connexion console css cyanogen decrypt démasquées démasquer développement dll dump easter eggs écologie écrit employeur EMUI EMUI5.0 encrypt enfant évènement export-package ExtJS fab fastboot fiche find firefox gadget galaxytab gelf gem git gmail gnupg gooelappengine google gparted gpg gpg2 gps graylog grenoble Grid gui harddrive heroku hover howto HTML http https IE ihm immobilier imprimante innovation insolite instance integration Java JavaScript jenkins jeu jobs json json-schema-validator key keystore labs linux livre log log4j logger logs lombok masquées masquer maven maven-gae-plugin Mémoire microsoft mobile mockito mondialisation monitor MUSE musique en ligne myopera nodejs npm NT NTEventLogger onglet openstack osgi paas package parameters parent php politique prosyst prototype proxies proxy quartz radio rappel recherche regex repository resize RIA ridge rock ROM route ruby rubygems s8500 samsung scheduler scm secret secure sel selenium Serializer server shared shell sign signature slf4j smartphone so société song spy ssh ssl struct swagger swig tâches téléphone téléréalité test thunderbird timeout token Tomcat tooltip tooltips truststore TWRP ubuntu unit test validator verify virgin virtualbox wave waze web WebApp wiki wikimedia wikipédia wikipen wiko windows yahoo youtube yum