1 /* 2 * Copyright (c) 2018 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.mockitoutil.async; 6 7 import static java.util.concurrent.TimeUnit.MILLISECONDS; 8 9 import static org.junit.Assert.assertEquals; 10 import static org.mockitoutil.Stopwatch.createNotStarted; 11 12 import java.util.concurrent.atomic.AtomicInteger; 13 14 import org.junit.After; 15 import org.junit.Test; 16 import org.mockitoutil.Stopwatch; 17 18 public class AsyncTestingTest { 19 20 private AsyncTesting async = new AsyncTesting(); 21 private Stopwatch watch = createNotStarted(); 22 23 @After after()24 public void after() { 25 async.cleanUp(); 26 } 27 28 @Test sanity_test()29 public void sanity_test() { 30 // given 31 watch.start(); 32 final AtomicInteger value = new AtomicInteger(0); 33 34 // when 35 async.runAfter( 36 200, 37 new Runnable() { 38 public void run() { 39 value.incrementAndGet(); 40 } 41 }); 42 43 // then the runnable is truly async and has not ran yet: 44 assertEquals(0, value.get()); 45 46 // after some wait... 47 watch.waitFor(300); 48 49 // we actually waited for some time 50 watch.assertElapsedTimeIsMoreThan(200, MILLISECONDS); 51 52 // and the async has actually ran: 53 assertEquals(1, value.get()); 54 } 55 } 56