• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 org.junit.After;
8 import org.junit.Test;
9 import org.mockitoutil.Stopwatch;
10 
11 import java.util.concurrent.atomic.AtomicInteger;
12 
13 import static java.util.concurrent.TimeUnit.MILLISECONDS;
14 import static org.junit.Assert.assertEquals;
15 import static org.mockitoutil.Stopwatch.createNotStarted;
16 
17 public class AsyncTestingTest {
18 
19     private AsyncTesting async = new AsyncTesting();
20     private Stopwatch watch = createNotStarted();
21 
22     @After
after()23     public void after() {
24         async.cleanUp();
25     }
26 
27     @Test
sanity_test()28     public void sanity_test() {
29         //given
30         watch.start();
31         final AtomicInteger value = new AtomicInteger(0);
32 
33         //when
34         async.runAfter(200, new Runnable() {
35             public void run() {
36                 value.incrementAndGet();
37             }
38         });
39 
40         //then the runnable is truly async and has not ran yet:
41         assertEquals(0, value.get());
42 
43         //after some wait...
44         watch.waitFor(300);
45 
46         //we actually waited for some time
47         watch.assertElapsedTimeIsMoreThan(200, MILLISECONDS);
48 
49         //and the async has actually ran:
50         assertEquals(1, value.get());
51     }
52 }
53