Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/main/java/org/junit/rules/Stopwatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* The Stopwatch Rule notifies one of its own protected methods of the time spent by a test.<p/>
* Override them to get the time in nanoseconds. For example, this class will keep logging the
* time spent by each passing, failing and skipped test:
* time spent by each passed, failed, skipped, and finished test:
*
* <pre>
* public static class StopwatchTest {
Expand Down Expand Up @@ -58,13 +58,33 @@
* }
* </pre>
*
* An example to assert runtime:
* <pre>
* &#064;Test
* public void performanceTest() throws InterruptedException {
* long delta= 30;
* Thread.sleep(300L);
* assertEquals(stopwatch.runtime(MILLISECONDS), 300d, delta);
* Thread.sleep(500L);
* assertEquals(stopwatch.runtime(MILLISECONDS), 800d, delta);
* }
* </pre>
*
* @author tibor17
* @since 4.12
*/
public class Stopwatch extends TestWatcher {
private long fStartNanos;
private long fEndNanos;

/**
* @param unit time unit for returned runtime
* @return runtime measured during the test
*/
public long runtime(TimeUnit unit) {
return unit.convert(currentNanoTime() - fStartNanos, TimeUnit.NANOSECONDS);
}

/**
* Invoked when a test succeeds
*/
Expand Down Expand Up @@ -118,11 +138,15 @@ private long getNanos() {
}

private void starting() {
fStartNanos= System.nanoTime();
fStartNanos= currentNanoTime();
}

private void stopping() {
fEndNanos= System.nanoTime();
fEndNanos= currentNanoTime();
}

private long currentNanoTime() {
return System.nanoTime();
}

@Override final protected void succeeded(Description description) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;

import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assume.assumeTrue;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertNotEquals;

/**
* @author tibor17
Expand Down Expand Up @@ -91,6 +93,24 @@ public void skippedTest() {
}
}

public static class WrongDurationTest extends AbstractStopwatchTest {
@Test
public void wrongDuration() throws InterruptedException {
Thread.sleep(500L);
assertNotEquals(fStopwatch.runtime(MILLISECONDS), 300d, 100d);
}
}

public static class DurationTest extends AbstractStopwatchTest {
@Test
public void duration() throws InterruptedException {
Thread.sleep(300L);
assertEquals(fStopwatch.runtime(MILLISECONDS), 300d, 100d);
Thread.sleep(500L);
assertEquals(fStopwatch.runtime(MILLISECONDS), 800d, 250d);
}
}

@Before
public void init() {
fRecord= new Record();
Expand Down Expand Up @@ -129,4 +149,16 @@ public void skipped() {
assertTrue("timeSpent > 0", fRecord.fDuration > 0);
assertThat(fRecord.fDuration, is(fFinishedRecord.fDuration));
}

@Test
public void wrongDuration() {
Result result= JUnitCore.runClasses(WrongDurationTest.class);
assertTrue(result.wasSuccessful());
}

@Test
public void duration() {
Result result= JUnitCore.runClasses(DurationTest.class);
assertTrue(result.wasSuccessful());
}
}