jeudi 5 mai 2016

Program termination during quartz scheduler verification using power mockito

I am trying to verify the quartz scheduler process by mocking System.currentTimeMillis() using PowerMockito .

Scenario :

I have scheduled a job for May 6th 2016 12 hrs .For verification I am trying to mock the System.currentTimeMillis() to scheduled time. So, the quartz will trigger on that particular time.

Test Case :

 @RunWith(PowerMockRunner.class)
 @PrepareForTest({ QuartzSchedulerThread.class })
 public class SampleExampleTest {

   @Test
   public void test() {
      /**
       * mock the system.current milliseconds
       */
     PowerMockito.mockStatic(System.class);
     long value=getMockedMilliseconds();
     PowerMockito.when(System.currentTimeMillis()).thenReturn(value);

     System.out.println("Mocked milliseconds"+value);
     try {

         SchedulerFactory sf = new StdSchedulerFactory(getProperties());
         Scheduler sched = sf.getScheduler();
         sched.start();

     } catch (SchedulerException e) {
         e.printStackTrace();
    } catch (Exception e) {         
        e.printStackTrace();
    }
}

public  long getMockedMilliseconds() {        
    Date expectedDate=new Date(116, 4, 6 , 11, 57);
    long mokedMilliSeconds = expectedDate.getTime();
    return mokedMilliSeconds;
  }

 public Properties getProperties(){

    Properties properties = new Properties();
    properties.setProperty("org.quartz.scheduler.skipUpdateCheck", "true");

    // set other properties ...such as
    properties.setProperty("org.quartz.jobStore.class",
            "org.quartz.impl.jdbcjobstore.JobStoreTX");
    properties.setProperty("org.quartz.jobStore.driverDelegateClass",
            "org.quartz.impl.jdbcjobstore.PostgreSQLDelegate");
    properties.setProperty("org.quartz.jobStore.tablePrefix", "QRTZ_");
    properties.setProperty("org.quartz.jobStore.dataSource", "obulis");
    //properties.setProperty("org.quartz.jobStore.misfireThreshold", "1000");

    // Datasource configurations
    properties.setProperty("org.quartz.dataSource.obulis.driver",
            "org.postgresql.Driver");
    properties.setProperty("org.quartz.dataSource.obulis.URL",
            "jdbc:postgresql://192.168.27.43:5433/obulis");
    properties.setProperty("org.quartz.dataSource.obulis.user", "postgres");
    properties.setProperty("org.quartz.dataSource.obulis.password",
            "Nuwaza123");
    properties.setProperty("org.quartz.dataSource.obulis.maxConnections",
            "5");
    properties.setProperty("rg.quartz.dataSource.obulis.validationQuery",
            "select 0");

    properties.setProperty("org.quartz.threadPool.class",
            "org.quartz.simpl.SimpleThreadPool");
    properties.setProperty("org.quartz.threadPool.threadCount", "4");

    return properties;
}
 }

Problem:

While executing the test case the program terminates abruptly. If the PowerMock is removed it is starting the scheduler.

console:

   Mocked milliseconds1462516020000
   INFO [main] (MLog.java:80) - MLog clients using log4j logging.
   INFO [main] (C3P0Registry.java:204) - Initializing c3p0-0.9.1.1 [built 15-    March-2007 01:32:31; debug? true; trace: 10]
   Picked up _JAVA_OPTIONS: -Djava.net.preferIPv4Stack=true

What will be the cause of it? How to mock and verify whether the job will trigger on that particular time ?

Aucun commentaire:

Enregistrer un commentaire