Best junit code snippet using org.junit.rules.Stopwatch.succeeded
Source:StopwatchTest.java  
...54            }55        };56        protected final Stopwatch stopwatch = new Stopwatch(fakeClock) {57            @Override58            protected void succeeded(long nanos, Description description) {59                StopwatchTest.record = new Record(nanos, TestStatus.SUCCEEDED, description);60                simulateTimePassing(1);61            }62            @Override63            protected void failed(long nanos, Throwable e, Description description) {64                StopwatchTest.record = new Record(nanos, TestStatus.FAILED, description);65                simulateTimePassing(1);66            }67            @Override68            protected void skipped(long nanos, AssumptionViolatedException e, Description description) {69                StopwatchTest.record = new Record(nanos, TestStatus.SKIPPED, description);70                simulateTimePassing(1);71            }72            @Override73            protected void finished(long nanos, Description description) {74                StopwatchTest.finishedRecord = new Record(nanos, description);75            }76        };77        private final TestWatcher watcher = new TestWatcher() {78            @Override79            protected void finished(Description description) {80                afterStopwatchRule();81            }82        };83        @Rule84        public final RuleChain chain = RuleChain85            .outerRule(watcher)86            .around(stopwatch);87        protected void afterStopwatchRule() {88        }89    }90    public static class SuccessfulTest extends AbstractStopwatchTest {91        @Test92        public void successfulTest() {93        }94    }95    public static class FailedTest extends AbstractStopwatchTest {96        @Test97        public void failedTest() {98            fail();99        }100    }101    public static class SkippedTest extends AbstractStopwatchTest {102        @Test103        public void skippedTest() {104            assumeTrue(false);105        }106    }107    public static class DurationDuringTestTest extends AbstractStopwatchTest {108        @Test109        public void duration() {110            simulateTimePassing(300L);111            assertEquals(300L, stopwatch.runtime(MILLISECONDS));112            simulateTimePassing(500L);113            assertEquals(800L, stopwatch.runtime(MILLISECONDS));114        }115    }116    public static class DurationAfterTestTest extends AbstractStopwatchTest {117        @Test118        public void duration() {119            simulateTimePassing(300L);120            assertEquals(300L, stopwatch.runtime(MILLISECONDS));121        }122        @Override123        protected void afterStopwatchRule() {124            assertEquals(300L, stopwatch.runtime(MILLISECONDS));125            simulateTimePassing(500L);126            assertEquals(300L, stopwatch.runtime(MILLISECONDS));127        }128    }129    @Before130    public void init() {131        record = new Record();132        finishedRecord = new Record();133        simulateTimePassing(1L);134    }135    private static Result runTest(Class<?> test) {136        simulateTimePassing(1L);137        JUnitCore junitCore = new JUnitCore();138        return junitCore.run(Request.aClass(test).getRunner());139    }140    private static void simulateTimePassing(long millis) {141        fakeTimeNanos += TimeUnit.MILLISECONDS.toNanos(millis);142    }143    @Test144    public void succeeded() {145        Result result = runTest(SuccessfulTest.class);146        assertEquals(0, result.getFailureCount());147        assertThat(record.name, is("successfulTest"));148        assertThat(record.name, is(finishedRecord.name));149        assertThat(record.status, is(TestStatus.SUCCEEDED));150        assertTrue("timeSpent > 0", record.duration > 0);151        assertThat(record.duration, is(finishedRecord.duration));152    }153    @Test154    public void failed() {155        Result result = runTest(FailedTest.class);156        assertEquals(1, result.getFailureCount());157        assertThat(record.name, is("failedTest"));158        assertThat(record.name, is(finishedRecord.name));...Source:Stopwatch.java  
...16    public long runtime(TimeUnit unit) {17        return unit.convert(getNanos(), TimeUnit.NANOSECONDS);18    }19    /* access modifiers changed from: protected */20    public void succeeded(long nanos, Description description) {21    }22    /* access modifiers changed from: protected */23    public void failed(long nanos, Throwable e, Description description) {24    }25    /* access modifiers changed from: protected */26    public void skipped(long nanos, AssumptionViolatedException e, Description description) {27    }28    /* access modifiers changed from: protected */29    public void finished(long nanos, Description description) {30    }31    /* access modifiers changed from: private */32    /* access modifiers changed from: public */33    private long getNanos() {34        if (this.startNanos != 0) {35            long currentEndNanos = this.endNanos;36            if (currentEndNanos == 0) {37                currentEndNanos = this.clock.nanoTime();38            }39            return currentEndNanos - this.startNanos;40        }41        throw new IllegalStateException("Test has not started");42    }43    /* access modifiers changed from: private */44    /* access modifiers changed from: public */45    private void starting() {46        this.startNanos = this.clock.nanoTime();47        this.endNanos = 0;48    }49    /* access modifiers changed from: private */50    /* access modifiers changed from: public */51    private void stopping() {52        this.endNanos = this.clock.nanoTime();53    }54    @Override // org.junit.rules.TestRule55    public final Statement apply(Statement base, Description description) {56        return new InternalWatcher().apply(base, description);57    }58    private class InternalWatcher extends TestWatcher {59        private InternalWatcher() {60        }61        /* access modifiers changed from: protected */62        @Override // org.junit.rules.TestWatcher63        public void starting(Description description) {64            Stopwatch.this.starting();65        }66        /* access modifiers changed from: protected */67        @Override // org.junit.rules.TestWatcher68        public void finished(Description description) {69            Stopwatch stopwatch = Stopwatch.this;70            stopwatch.finished(stopwatch.getNanos(), description);71        }72        /* access modifiers changed from: protected */73        @Override // org.junit.rules.TestWatcher74        public void succeeded(Description description) {75            Stopwatch.this.stopping();76            Stopwatch stopwatch = Stopwatch.this;77            stopwatch.succeeded(stopwatch.getNanos(), description);78        }79        /* access modifiers changed from: protected */80        @Override // org.junit.rules.TestWatcher81        public void failed(Throwable e, Description description) {82            Stopwatch.this.stopping();83            Stopwatch stopwatch = Stopwatch.this;84            stopwatch.failed(stopwatch.getNanos(), e, description);85        }86        /* access modifiers changed from: protected */87        @Override // org.junit.rules.TestWatcher88        public void skipped(AssumptionViolatedException e, Description description) {89            Stopwatch.this.stopping();90            Stopwatch stopwatch = Stopwatch.this;91            stopwatch.skipped(stopwatch.getNanos(), e, description);...Source:QuickFindUnionTest.java  
...25    public ExpectedException expectedException = ExpectedException.none();26    @Rule27    public Stopwatch stopwatch = new Stopwatch() {28        @Override29        protected void succeeded(long nanos, Description description) {30            logInfo(description, "succeeded", nanos);31        }32        @Override33        protected void failed(long nanos, Throwable e, Description description) {34            logInfo(description, "failed", nanos);35        }36        @Override37        protected void finished(long nanos, Description description) {38            logInfo(description, "finished", nanos);39        }40    };41    public void testFind(QuickFindUnion quickFind, int size) throws Exception {42        for (int i = 0; i < size; i++) {43            assertTrue(quickFind.find(i, i));44            for (int j = i + 1; j < size; j++) {...Source:StopwatchRule.java  
...53        mInstrumentation.sendStatus(INST_STATUS_IN_PROGRESS, mResult);54    }55    /** {@inheritDoc} */56    @Override57    protected void succeeded(long nanos, Description description) {58        reportMetric(nanos, description);59    }60    @VisibleForTesting61    Bundle getMetric() {62        return mResult;63    }64    @VisibleForTesting65    void setInstrumentation(Instrumentation instrumentation) {66        mInstrumentation = instrumentation;67    }68}...Source:PerfTest.java  
...33	}34	@Rule35	public Stopwatch stopwatch = new Stopwatch() {36		@Override37		protected void succeeded(long nanos, Description description) {38			logInfo(description, "succeeded", nanos);39		}40		@Override41		protected void failed(long nanos, Throwable e, Description description) {42			logInfo(description, "failed", nanos);43		}44		@Override45		protected void skipped(long nanos, AssumptionViolatedException e,46				Description description) {47			logInfo(description, "skipped", nanos);48		}49		@Override50		protected void finished(long nanos, Description description) {51			// logInfo(description, "finished", nanos);52		}...Source:Watch.java  
...4import org.junit.runner.Description;5import java.util.concurrent.TimeUnit;6public class Watch extends Stopwatch {7    @Override8    protected void succeeded(long nanos, Description description) {9        logInfo(description, "succeeded", nanos);10    }11    @Override12    protected void failed(long nanos, Throwable e, Description description) {13        logInfo(description, "failed", nanos);14    }15    @Override16    protected void skipped(long nanos, AssumptionViolatedException e, Description description) {17        logInfo(description, "skipped", nanos);18    }19    private static void logInfo(Description description, String status, long nanos) {20        String testName = description.getMethodName();21        System.out.println(String.format("Test [%s] %s, spent %d ms",22                testName, status, TimeUnit.NANOSECONDS.toMillis(nanos)));23    }...LambdaTest also has a detailed JUnit tutorial explaining its features, importance, advanced use cases, best practices, and more to help you get started with running your automation testing scripts.
Here are the detailed JUnit testing chapters to help you get started:
You can also check out our JUnit certification if you wish to take your career in Selenium automation testing with JUnit to the next level.
Get 100 minutes of automation test minutes FREE!!
