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 package org.mockito.verification;
      6 
      7 import org.junit.Test;
      8 import org.mockito.InOrder;
      9 import org.mockito.Mock;
     10 import org.mockito.exceptions.base.MockitoAssertionError;
     11 import org.mockito.internal.util.Timer;
     12 import org.mockito.internal.verification.VerificationDataImpl;
     13 import org.mockitoutil.TestBase;
     14 
     15 import static org.junit.Assert.fail;
     16 import static org.mockito.Mockito.*;
     17 
     18 public class TimeoutTest extends TestBase {
     19 
     20     @Mock
     21     VerificationMode mode;
     22     @Mock
     23     VerificationDataImpl data;
     24     @Mock
     25     Timer timer;
     26 
     27     private final MockitoAssertionError error = new MockitoAssertionError("");
     28 
     29     @Test
     30     public void should_pass_when_verification_passes() {
     31         Timeout t = new Timeout(1, mode, timer);
     32 
     33         when(timer.isCounting()).thenReturn(true);
     34         doNothing().when(mode).verify(data);
     35 
     36         t.verify(data);
     37 
     38         InOrder inOrder = inOrder(timer);
     39         inOrder.verify(timer).start();
     40         inOrder.verify(timer).isCounting();
     41     }
     42 
     43     @Test
     44     public void should_fail_because_verification_fails() {
     45         Timeout t = new Timeout(1, mode, timer);
     46 
     47         when(timer.isCounting()).thenReturn(true, true, true, false);
     48         doThrow(error).
     49         doThrow(error).
     50         doThrow(error).
     51         when(mode).verify(data);
     52 
     53         try {
     54             t.verify(data);
     55             fail();
     56         } catch (MockitoAssertionError e) {}
     57 
     58         verify(timer, times(4)).isCounting();
     59     }
     60 
     61     @Test
     62     public void should_pass_even_if_first_verification_fails() {
     63         Timeout t = new Timeout(1, mode, timer);
     64 
     65         when(timer.isCounting()).thenReturn(true, true, true, false);
     66         doThrow(error).
     67         doThrow(error).
     68         doNothing().
     69         when(mode).verify(data);
     70 
     71         t.verify(data);
     72         verify(timer, times(3)).isCounting();
     73     }
     74 
     75     @Test
     76     public void should_try_to_verify_correct_number_of_times() {
     77         Timeout t = new Timeout(10, mode, timer);
     78 
     79         doThrow(error).when(mode).verify(data);
     80         when(timer.isCounting()).thenReturn(true, true, true, true, true, false);
     81 
     82         try {
     83             t.verify(data);
     84             fail();
     85         } catch (MockitoAssertionError e) {}
     86 
     87         verify(mode, times(5)).verify(data);
     88     }
     89 
     90 }
     91