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.ThrowableAssert; 9 import org.junit.Test; 10 import org.mockito.Mock; 11 import org.mockito.exceptions.verification.NoInteractionsWanted; 12 import org.mockito.exceptions.verification.TooManyActualInvocations; 13 import org.mockito.exceptions.verification.WantedButNotInvoked; 14 import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent; 15 import org.mockitousage.IMethods; 16 import org.mockitoutil.TestBase; 17 18 import java.util.List; 19 20 import static org.assertj.core.api.Assertions.assertThatThrownBy; 21 import static org.junit.Assert.fail; 22 import static org.mockito.Mockito.*; 23 24 public class BasicVerificationTest extends TestBase { 25 26 @Mock private List<String> mock; 27 @Mock private List<String> mockTwo; 28 29 @Test 30 public void shouldVerify() throws Exception { 31 mock.clear(); 32 verify(mock).clear(); 33 34 mock.add("test"); 35 verify(mock).add("test"); 36 37 verifyNoMoreInteractions(mock); 38 } 39 40 @Test(expected=WantedButNotInvoked.class) 41 public void shouldFailVerification() throws Exception { 42 verify(mock).clear(); 43 } 44 45 @Test 46 public void shouldFailVerificationOnMethodArgument() throws Exception { 47 mock.clear(); 48 mock.add("foo"); 49 50 verify(mock).clear(); 51 52 assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { 53 @Override 54 public void call() { 55 verify(mock).add("bar"); 56 } 57 }).isInstanceOf(ArgumentsAreDifferent.class); 58 } 59 60 @Test 61 public void shouldFailOnWrongMethod() throws Exception { 62 mock.clear(); 63 mock.clear(); 64 65 mockTwo.add("add"); 66 67 verify(mock, atLeastOnce()).clear(); 68 verify(mockTwo, atLeastOnce()).add("add"); 69 try { 70 verify(mockTwo, atLeastOnce()).add("foo"); 71 fail(); 72 } catch (WantedButNotInvoked e) {} 73 } 74 75 @Test 76 public void shouldDetectRedundantInvocation() throws Exception { 77 mock.clear(); 78 mock.add("foo"); 79 mock.add("bar"); 80 81 verify(mock).clear(); 82 verify(mock).add("foo"); 83 84 try { 85 verifyNoMoreInteractions(mock); 86 fail(); 87 } catch (NoInteractionsWanted e) {} 88 } 89 90 @Test 91 public void shouldDetectWhenInvokedMoreThanOnce() throws Exception { 92 mock.add("foo"); 93 mock.clear(); 94 mock.clear(); 95 96 verify(mock).add("foo"); 97 98 try { 99 verify(mock).clear(); 100 fail(); 101 } catch (TooManyActualInvocations e) {} 102 } 103 104 @Test 105 public void shouldVerifyStubbedMethods() throws Exception { 106 when(mock.add("test")).thenReturn(Boolean.FALSE); 107 108 mock.add("test"); 109 110 verify(mock).add("test"); 111 } 112 113 114 @Test 115 public void shouldDetectWhenOverloadedMethodCalled() throws Exception { 116 IMethods mockThree = mock(IMethods.class); 117 118 mockThree.varargs((Object[]) new Object[] {}); 119 try { 120 verify(mockThree).varargs((String[]) new String[] {}); 121 fail(); 122 } catch(WantedButNotInvoked e) {} 123 } 124 } 125