public class Stopwatch extends TestWatcher
public static class StopwatchTest { private static final Logger logger= Logger.getLogger(""); private static void logInfo(String testName, String status, long nanos) { logger.info(String.format("Test %s %s, spent %d microseconds", testName, status, Stopwatch.toMicros(nanos))); } @Rule public Stopwatch stopwatch= new Stopwatch() { @Override protected void succeeded(long nanos, Description description) { logInfo(description.getMethodName(), "succeeded", nanos); } @Override protected void failed(long nanos, Throwable e, Description description) { logInfo(description.getMethodName(), "failed", nanos); } @Override protected void skipped(long nanos, AssumptionViolatedException e, Description description) { logInfo(description.getMethodName(), "skipped", nanos); } @Override protected void finished(long nanos, Description description) { logInfo(description.getMethodName(), "finished", nanos); } }; @Test public void succeeds() { } @Test public void fails() { fail(); } @Test public void skips() { assumeTrue(false); } }An example to assert runtime:
@Test public void performanceTest() throws InterruptedException { long delta= 30; Thread.sleep(300L); assertEquals(300d, stopwatch.runtime(MILLISECONDS), delta); Thread.sleep(500L); assertEquals(800d, stopwatch.runtime(MILLISECONDS), delta); }
Constructor and Description |
---|
Stopwatch() |
Modifier and Type | Method and Description |
---|---|
protected void |
failed(long nanos,
Throwable e,
Description description)
Invoked when a test fails
|
protected void |
failed(Throwable e,
Description description)
Invoked when a test fails
|
protected void |
finished(Description description)
Invoked when a test method finishes (whether passing or failing)
|
protected void |
finished(long nanos,
Description description)
Invoked when a test method finishes (whether passing or failing)
|
long |
runtime(TimeUnit unit) |
protected void |
skipped(org.junit.internal.AssumptionViolatedException e,
Description description)
Invoked when a test is skipped due to a failed assumption.
|
protected void |
skipped(long nanos,
org.junit.internal.AssumptionViolatedException e,
Description description)
Invoked when a test is skipped due to a failed assumption.
|
protected void |
starting(Description description)
Invoked when a test is about to start
|
protected void |
succeeded(Description description)
Invoked when a test succeeds
|
protected void |
succeeded(long nanos,
Description description)
Invoked when a test succeeds
|
static long |
toMicros(long nanos) |
static long |
toMillis(long nanos) |
static long |
toSeconds(long nanos) |
apply
public Stopwatch()
public long runtime(TimeUnit unit)
unit
- time unit for returned runtimeprotected void succeeded(long nanos, Description description)
protected void failed(long nanos, Throwable e, Description description)
protected void skipped(long nanos, org.junit.internal.AssumptionViolatedException e, Description description)
protected void finished(long nanos, Description description)
public static long toMicros(long nanos)
nanos
- time in nanosecondspublic static long toMillis(long nanos)
nanos
- time in nanosecondspublic static long toSeconds(long nanos)
nanos
- time in nanosecondsprotected final void succeeded(Description description)
TestWatcher
succeeded
in class TestWatcher
protected final void failed(Throwable e, Description description)
TestWatcher
failed
in class TestWatcher
protected final void skipped(org.junit.internal.AssumptionViolatedException e, Description description)
TestWatcher
skipped
in class TestWatcher
protected final void starting(Description description)
TestWatcher
starting
in class TestWatcher
protected final void finished(Description description)
TestWatcher
finished
in class TestWatcher
Copyright © 2002-2012 JUnit. All Rights Reserved.