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.junit.Before; 9 import org.junit.Test; 10 import org.mockito.exceptions.base.MockitoException; 11 import org.mockito.exceptions.verification.NoInteractionsWanted; 12 import org.mockitoutil.TestBase; 13 14 import java.util.LinkedList; 15 import java.util.List; 16 import java.util.Map; 17 18 import static org.junit.Assert.fail; 19 import static org.assertj.core.api.Assertions.assertThat; 20 import static org.mockito.Mockito.*; 21 22 @SuppressWarnings("unchecked") 23 public class NoMoreInteractionsVerificationTest extends TestBase { 24 25 private LinkedList mock; 26 27 @Before 28 public void setup() { 29 mock = mock(LinkedList.class); 30 } 31 32 @Test 33 public void shouldStubbingNotRegisterRedundantInteractions() throws Exception { 34 when(mock.add("one")).thenReturn(true); 35 when(mock.add("two")).thenReturn(true); 36 37 mock.add("one"); 38 39 verify(mock).add("one"); 40 verifyNoMoreInteractions(mock); 41 } 42 43 @Test 44 public void shouldVerifyWhenWantedNumberOfInvocationsUsed() throws Exception { 45 mock.add("one"); 46 mock.add("one"); 47 mock.add("one"); 48 49 verify(mock, times(3)).add("one"); 50 51 verifyNoMoreInteractions(mock); 52 } 53 54 @Test 55 public void shouldVerifyNoInteractionsAsManyTimesAsYouWant() throws Exception { 56 verifyNoMoreInteractions(mock); 57 verifyNoMoreInteractions(mock); 58 59 verifyZeroInteractions(mock); 60 verifyZeroInteractions(mock); 61 } 62 63 @Test 64 public void shouldFailZeroInteractionsVerification() throws Exception { 65 mock.clear(); 66 67 try { 68 verifyZeroInteractions(mock); 69 fail(); 70 } catch (NoInteractionsWanted e) {} 71 } 72 73 @Test 74 public void shouldFailNoMoreInteractionsVerification() throws Exception { 75 mock.clear(); 76 77 try { 78 verifyNoMoreInteractions(mock); 79 fail(); 80 } catch (NoInteractionsWanted e) {} 81 } 82 83 @Test 84 public void shouldPrintAllInvocationsWhenVerifyingNoMoreInvocations() throws Exception { 85 mock.add(1); 86 mock.add(2); 87 mock.clear(); 88 89 verify(mock).add(2); 90 try { 91 verifyNoMoreInteractions(mock); 92 fail(); 93 } catch (NoInteractionsWanted e) { 94 assertThat(e).hasMessageContaining("list of all invocations"); 95 } 96 } 97 98 @Test 99 public void shouldNotContainAllInvocationsWhenSingleUnwantedFound() throws Exception { 100 mock.add(1); 101 102 try { 103 verifyNoMoreInteractions(mock); 104 fail(); 105 } catch (NoInteractionsWanted e) { 106 assertThat(e.getMessage()).doesNotContain("list of all invocations"); 107 } 108 } 109 110 @Test 111 public void shouldVerifyOneMockButFailOnOther() throws Exception { 112 List<String> list = mock(List.class); 113 Map<String, Integer> map = mock(Map.class); 114 115 list.add("one"); 116 list.add("one"); 117 118 map.put("one", 1); 119 120 verify(list, times(2)).add("one"); 121 122 verifyNoMoreInteractions(list); 123 try { 124 verifyZeroInteractions(map); 125 fail(); 126 } catch (NoInteractionsWanted e) {} 127 } 128 129 @SuppressWarnings("all") 130 @Test(expected=MockitoException.class) 131 public void verifyNoMoreInteractionsShouldScreamWhenNullPassed() throws Exception { 132 verifyNoMoreInteractions((Object[])null); 133 } 134 } 135