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.InOrder; 11 import org.mockito.Mockito; 12 import org.mockito.exceptions.verification.VerificationInOrderFailure; 13 import org.mockito.exceptions.verification.WantedButNotInvoked; 14 import org.mockitousage.IMethods; 15 import org.mockitoutil.TestBase; 16 17 import static junit.framework.TestCase.fail; 18 import static org.assertj.core.api.Assertions.assertThat; 19 import static org.mockito.Mockito.*; 20 21 public class DescriptiveMessagesOnVerificationInOrderErrorsTest extends TestBase { 22 23 private IMethods one; 24 private IMethods two; 25 private IMethods three; 26 private InOrder inOrder; 27 28 @Before 29 public void setup() { 30 one = Mockito.mock(IMethods.class); 31 two = Mockito.mock(IMethods.class); 32 three = Mockito.mock(IMethods.class); 33 34 one.simpleMethod(1); 35 one.simpleMethod(11); 36 two.simpleMethod(2); 37 two.simpleMethod(2); 38 three.simpleMethod(3); 39 40 inOrder = inOrder(one, two, three); 41 } 42 43 @Test 44 public void shouldPrintVerificationInOrderErrorAndShowBothWantedAndPrevious() { 45 inOrder.verify(one).simpleMethod(1); 46 inOrder.verify(two, atLeastOnce()).simpleMethod(2); 47 48 try { 49 inOrder.verify(one, atLeastOnce()).simpleMethod(11); 50 fail(); 51 } catch (VerificationInOrderFailure e) { 52 String expected = 53 "\n" + 54 "Verification in order failure" + 55 "\n" + 56 "Wanted but not invoked:" + 57 "\n" + 58 "iMethods.simpleMethod(11);" + 59 "\n" + 60 "-> at "; 61 62 assertThat(e).hasMessageContaining(expected); 63 64 String expectedCause = 65 "\n" + 66 "Wanted anywhere AFTER following interaction:" + 67 "\n" + 68 "iMethods.simpleMethod(2);" + 69 "\n" + 70 "-> at "; 71 72 assertThat(e).hasMessageContaining(expectedCause); 73 } 74 } 75 76 @Test 77 public void shouldPrintVerificationInOrderErrorAndShowWantedOnly() { 78 try { 79 inOrder.verify(one).differentMethod(); 80 fail(); 81 } catch (WantedButNotInvoked e) { 82 String expected = 83 "\n" + 84 "Wanted but not invoked:" + 85 "\n" + 86 "iMethods.differentMethod();" + 87 "\n" + 88 "-> at"; 89 90 assertThat(e).hasMessageContaining(expected); 91 } 92 } 93 94 @Test 95 public void shouldPrintVerificationInOrderErrorAndShowWantedAndActual() { 96 try { 97 inOrder.verify(one).simpleMethod(999); 98 fail(); 99 } catch (org.mockito.exceptions.verification.junit.ArgumentsAreDifferent e) { 100 assertThat(e).hasMessageContaining("has different arguments"); 101 } 102 } 103 104 @Test 105 public void shouldNotSayArgumentsAreDifferent() { 106 //this is the last invocation so any next verification in order should simply say wanted but not invoked 107 inOrder.verify(three).simpleMethod(3); 108 try { 109 inOrder.verify(one).simpleMethod(999); 110 fail(); 111 } catch (VerificationInOrderFailure e) { 112 assertThat(e).hasMessageContaining("Wanted but not invoked"); 113 } 114 } 115 116 @Test 117 public void shouldPrintMethodThatWasNotInvoked() { 118 inOrder.verify(one).simpleMethod(1); 119 inOrder.verify(one).simpleMethod(11); 120 inOrder.verify(two, times(2)).simpleMethod(2); 121 inOrder.verify(three).simpleMethod(3); 122 try { 123 inOrder.verify(three).simpleMethod(999); 124 fail(); 125 } catch (VerificationInOrderFailure e) { 126 String expectedMessage = 127 "\n" + 128 "Verification in order failure" + 129 "\n" + 130 "Wanted but not invoked:" + 131 "\n" + 132 "iMethods.simpleMethod(999);"; 133 assertThat(e).hasMessageContaining(expectedMessage); 134 } 135 } 136 137 @Test 138 public void shouldPrintTooManyInvocations() { 139 inOrder.verify(one).simpleMethod(1); 140 inOrder.verify(one).simpleMethod(11); 141 try { 142 inOrder.verify(two, times(1)).simpleMethod(2); 143 fail(); 144 } catch (VerificationInOrderFailure e) { 145 String expectedMessage = 146 "\n" + 147 "Verification in order failure:" + 148 "\n" + 149 "iMethods.simpleMethod(2);" + 150 "\n" + 151 "Wanted 1 time:" + 152 "\n" + 153 "-> at"; 154 assertThat(e).hasMessageContaining(expectedMessage); 155 156 String expectedCause = 157 "\n" + 158 "But was 2 times. Undesired invocation:" + 159 "\n" + 160 "-> at"; 161 assertThat(e).hasMessageContaining(expectedCause); 162 } 163 } 164 165 @Test 166 public void shouldPrintTooLittleInvocations() { 167 two.simpleMethod(2); 168 169 inOrder.verify(one, atLeastOnce()).simpleMethod(anyInt()); 170 inOrder.verify(two, times(2)).simpleMethod(2); 171 inOrder.verify(three, atLeastOnce()).simpleMethod(3); 172 173 try { 174 inOrder.verify(two, times(2)).simpleMethod(2); 175 fail(); 176 } catch (VerificationInOrderFailure e) { 177 String expectedMessage = 178 "\n" + 179 "Verification in order failure:" + 180 "\n" + 181 "iMethods.simpleMethod(2);" + 182 "\n" + 183 "Wanted 2 times:" + 184 "\n" + 185 "-> at"; 186 assertThat(e).hasMessageContaining(expectedMessage); 187 188 String expectedCause = 189 "\n" + 190 "But was 1 time:" + 191 "\n" + 192 "-> at"; 193 194 assertThat(e).hasMessageContaining(expectedCause); 195 } 196 } 197 } 198