Home | History | Annotate | Download | only in verification
      1 /*
      2  * Copyright (c) 2007 Mockito contributors
      3  * This program is made available under the terms of the MIT License.
      4  */
      5 
      6 package org.mockitousage.verification;
      7 
      8 import org.assertj.core.api.Assertions;
      9 import org.assertj.core.api.ThrowableAssert;
     10 import org.junit.After;
     11 import org.junit.Before;
     12 import org.junit.Ignore;
     13 import org.junit.Rule;
     14 import org.junit.Test;
     15 import org.mockito.Mock;
     16 import org.mockito.exceptions.verification.TooLittleActualInvocations;
     17 import org.mockito.junit.MockitoRule;
     18 import org.mockitousage.IMethods;
     19 import org.mockitoutil.Stopwatch;
     20 import org.mockitoutil.async.AsyncTesting;
     21 
     22 import java.util.concurrent.TimeUnit;
     23 
     24 import static org.mockito.Mockito.after;
     25 import static org.mockito.Mockito.timeout;
     26 import static org.mockito.Mockito.verify;
     27 import static org.mockito.junit.MockitoJUnit.rule;
     28 import static org.mockitoutil.Stopwatch.createNotStarted;
     29 
     30 public class VerificationWithTimeoutTest {
     31 
     32     @Rule public MockitoRule mockito = rule();
     33 
     34     private Stopwatch watch = createNotStarted();
     35 
     36     @Mock private IMethods mock;
     37 
     38     private AsyncTesting async;
     39 
     40     @Before
     41     public void setUp() {
     42         async = new AsyncTesting();
     43     }
     44 
     45     @After
     46     public void tearDown() {
     47         async.cleanUp();
     48     }
     49 
     50     @Test
     51     public void should_verify_with_timeout() {
     52         // when
     53         async.runAfter(50, callMock('c'));
     54         async.runAfter(500, callMock('c'));
     55 
     56         // then
     57         verify(mock, timeout(200).only()).oneArg('c');
     58         verify(mock).oneArg('c'); //sanity check
     59     }
     60 
     61     @Test
     62     public void should_verify_with_timeout_and_fail() {
     63         // when
     64         async.runAfter(200, callMock('c'));
     65 
     66         // then
     67         Assertions.assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {
     68             @Override
     69             public void call() {
     70                 verify(mock, timeout(50).only()).oneArg('c');
     71             }
     72         }).isInstanceOf(AssertionError.class).hasMessageContaining("Wanted but not invoked");
     73         //TODO let's have a specific exception vs. generic assertion error + message
     74     }
     75 
     76     @Test
     77     @Ignore //TODO nice to have
     78     public void should_verify_with_timeout_and_fail_early() {
     79         // when
     80         callMock('c');
     81         callMock('c');
     82 
     83         watch.start();
     84 
     85         // then
     86         Assertions.assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {
     87             @Override
     88             public void call() {
     89                 verify(mock, timeout(2000)).oneArg('c');
     90             }
     91         }).isInstanceOf(AssertionError.class).hasMessageContaining("Wanted but not invoked");
     92 
     93         watch.assertElapsedTimeIsLessThan(1000, TimeUnit.MILLISECONDS);
     94     }
     95 
     96     @Test
     97     public void should_verify_with_times_x() {
     98         // when
     99         async.runAfter(50, callMock('c'));
    100         async.runAfter(100, callMock('c'));
    101         async.runAfter(600, callMock('c'));
    102 
    103         // then
    104         verify(mock, timeout(300).times(2)).oneArg('c');
    105     }
    106 
    107     @Test
    108     public void should_verify_with_times_x_and_fail() {
    109         // when
    110         async.runAfter(10, callMock('c'));
    111         async.runAfter(200, callMock('c'));
    112 
    113         // then
    114         Assertions.assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {
    115             @Override
    116             public void call() {
    117                 verify(mock, timeout(100).times(2)).oneArg('c');
    118             }
    119         }).isInstanceOf(TooLittleActualInvocations.class);
    120     }
    121 
    122     @Test
    123     public void should_verify_with_at_least() {
    124         // when
    125         async.runAfter(10, callMock('c'));
    126         async.runAfter(50, callMock('c'));
    127 
    128         // then
    129         verify(mock, timeout(200).atLeast(2)).oneArg('c');
    130     }
    131 
    132     @Test
    133     public void should_verify_with_at_least_once() {
    134         // when
    135         async.runAfter(10, callMock('c'));
    136         async.runAfter(50, callMock('c'));
    137 
    138         // then
    139         verify(mock, timeout(200).atLeastOnce()).oneArg('c');
    140     }
    141 
    142     @Test
    143     public void should_verify_with_at_least_and_fail() {
    144         // when
    145         async.runAfter(10, callMock('c'));
    146         async.runAfter(50, callMock('c'));
    147 
    148         // then
    149         Assertions.assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {
    150             public void call() {
    151                 verify(mock, timeout(100).atLeast(3)).oneArg('c');
    152             }
    153         }).isInstanceOf(TooLittleActualInvocations.class);
    154     }
    155 
    156     @Test
    157     public void should_verify_with_only() {
    158         // when
    159         async.runAfter(10, callMock('c'));
    160         async.runAfter(300, callMock('c'));
    161 
    162         // then
    163         verify(mock, timeout(100).only()).oneArg('c');
    164     }
    165 
    166     @Test
    167     @Ignore("not testable, probably timeout().only() does not make sense")
    168     public void should_verify_with_only_and_fail() {
    169         // when
    170         async.runAfter(10, callMock('c'));
    171         async.runAfter(50, callMock('c'));
    172 
    173         // then
    174         Assertions.assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {
    175             @Override
    176             public void call() {
    177                 verify(mock, after(200).only()).oneArg('c');
    178             }
    179         }).isInstanceOf(AssertionError.class);
    180     }
    181 
    182     @Test
    183     @Ignore //TODO nice to have
    184     public void should_verify_with_only_and_fail_early() {
    185         // when
    186         callMock('c');
    187         callMock('c');
    188 
    189         watch.start();
    190 
    191         // then
    192         Assertions.assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {
    193             @Override
    194             public void call() {
    195                 verify(mock, timeout(2000).only()).oneArg('c');
    196             }
    197         }).isInstanceOf(AssertionError.class).hasMessageContaining("Wanted but not invoked"); //TODO specific exception
    198 
    199         watch.assertElapsedTimeIsLessThan(1000, TimeUnit.MILLISECONDS);
    200     }
    201 
    202     private Runnable callMock(final char c) {
    203         return new Runnable() {
    204             @Override
    205             public void run() {
    206                 mock.oneArg(c);
    207             }
    208         };
    209     }
    210 }
    211