0
Junit是否有一些特殊的註釋可以自動計算花費的時間?如果是這樣我才能避免手工計算的時間,像這樣:我該如何實現自定義秒錶?
long start = System.currentTimeMillis();
// do something here
long end = System.currentTimeMillis();
log.info("took time: {}", end - start);
assertTrue((end - start) > 1000L);
現在我覺得StopWatch
是有點麻煩:
@Rule
public Stopwatch stopwatch = new Stopwatch() {
@Override
protected void succeeded(long nanos, Description description) {
logInfo(description, "succeeded", nanos);
}
@Override
protected void failed(long nanos, Throwable e, Description description) {
logInfo(description, "failed", nanos);
}
@Override
protected void skipped(long nanos, AssumptionViolatedException e, Description description) {
logInfo(description, "skipped", nanos);
}
@Override
protected void finished(long nanos, Description description) {
logInfo(description, "finished", nanos);
}
};
理想情況下,我想這樣的效果:
@Test
@StopWatch
public void test_sleep_1_seconds(){
sleep1();
assertTrue(stopwatch.runtime(TimeUnit.SECONDS) > 1L);
}
如何我可以執行定製StopWatch
嗎?
謝謝!但是如果我想實現一個自定義的StopWatch註解 - MyStopWatch,並且直接在需求上面使用它的測試方法,就像'@ Test'' @ Before'可以'junit'通過實現一些類來支持它嗎? – zhuguowei
@zhuguowei這是可能的。我想你需要實現自定義[test runner](https://github.com/junit-team/junit4/wiki/Test-runners) –