PowerMockito
を使用してSystem.currentTimeMillis()
を嘲笑してクォーツスケジューラプロセスを検証しようとしています。パワーモチートを使用した石英スケジューラ検証中のプログラム終了
シナリオ:
私は予定時間にSystem.currentTimeMillis()
を模擬しようとしています5月6日2016年12時間.FOR検証のために仕事を予定しています。石英はその特定の時間にトリガします。
テストケース:
@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;
}
}
問題:
プログラムは、スケジューラを起動する前に、突然終了したテストケースを実行中。 PowerMock
が削除された場合、スケジューラが起動します。
コンソール:
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
それの原因になりますか?その特定の時間にジョブがトリガーされるかどうかを模擬して検証する方法は?