1 /* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.mockitousage.verification; 6 7 import static org.assertj.core.api.Assertions.assertThat; 8 import static org.junit.Assert.fail; 9 import static org.mockito.Mockito.*; 10 11 import org.junit.Before; 12 import org.junit.Test; 13 import org.mockito.InOrder; 14 import org.mockito.Mockito; 15 import org.mockito.exceptions.verification.VerificationInOrderFailure; 16 import org.mockito.exceptions.verification.WantedButNotInvoked; 17 import org.mockitousage.IMethods; 18 import org.mockitoutil.TestBase; 19 20 public class DescriptiveMessagesOnVerificationInOrderErrorsTest extends TestBase { 21 22 private IMethods one; 23 private IMethods two; 24 private IMethods three; 25 private InOrder inOrder; 26 27 @Before setup()28 public void setup() { 29 one = Mockito.mock(IMethods.class); 30 two = Mockito.mock(IMethods.class); 31 three = Mockito.mock(IMethods.class); 32 33 one.simpleMethod(1); 34 one.simpleMethod(11); 35 two.simpleMethod(2); 36 two.simpleMethod(2); 37 three.simpleMethod(3); 38 39 inOrder = inOrder(one, two, three); 40 } 41 42 @Test shouldPrintVerificationInOrderErrorAndShowBothWantedAndPrevious()43 public void shouldPrintVerificationInOrderErrorAndShowBothWantedAndPrevious() { 44 inOrder.verify(one).simpleMethod(1); 45 inOrder.verify(two, atLeastOnce()).simpleMethod(2); 46 47 try { 48 inOrder.verify(one, atLeastOnce()).simpleMethod(11); 49 fail(); 50 } catch (VerificationInOrderFailure e) { 51 String expected = 52 "\n" 53 + "Verification in order failure" 54 + "\n" 55 + "Wanted but not invoked:" 56 + "\n" 57 + "iMethods.simpleMethod(11);" 58 + "\n" 59 + "-> at "; 60 61 assertThat(e).hasMessageContaining(expected); 62 63 String expectedCause = 64 "\n" 65 + "Wanted anywhere AFTER following interaction:" 66 + "\n" 67 + "iMethods.simpleMethod(2);" 68 + "\n" 69 + "-> at "; 70 71 assertThat(e).hasMessageContaining(expectedCause); 72 } 73 } 74 75 @Test shouldPrintVerificationInOrderErrorAndShowWantedOnly()76 public void shouldPrintVerificationInOrderErrorAndShowWantedOnly() { 77 try { 78 inOrder.verify(one).differentMethod(); 79 fail(); 80 } catch (WantedButNotInvoked e) { 81 String expected = 82 "\n" 83 + "Wanted but not invoked:" 84 + "\n" 85 + "iMethods.differentMethod();" 86 + "\n" 87 + "-> at"; 88 89 assertThat(e).hasMessageContaining(expected); 90 } 91 } 92 93 @Test shouldPrintVerificationInOrderErrorAndShowWantedAndActual()94 public void shouldPrintVerificationInOrderErrorAndShowWantedAndActual() { 95 try { 96 inOrder.verify(one).simpleMethod(999); 97 fail(); 98 } catch (org.mockito.exceptions.verification.opentest4j.ArgumentsAreDifferent e) { 99 assertThat(e).hasMessageContaining("have different arguments"); 100 } 101 } 102 103 @Test shouldNotSayArgumentsAreDifferent()104 public void shouldNotSayArgumentsAreDifferent() { 105 // this is the last invocation so any next verification in order should simply say wanted 106 // 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 shouldPrintMethodThatWasNotInvoked()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 shouldPrintTooManyInvocations()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 = "\n" + "But was 2 times:" + "\n" + "-> at"; 157 assertThat(e).hasMessageContaining(expectedCause); 158 } 159 } 160 161 @Test shouldPrintTooFewInvocations()162 public void shouldPrintTooFewInvocations() { 163 two.simpleMethod(2); 164 165 inOrder.verify(one, atLeastOnce()).simpleMethod(anyInt()); 166 inOrder.verify(two, times(2)).simpleMethod(2); 167 inOrder.verify(three, atLeastOnce()).simpleMethod(3); 168 169 try { 170 inOrder.verify(two, times(2)).simpleMethod(2); 171 fail(); 172 } catch (VerificationInOrderFailure e) { 173 String expectedMessage = 174 "\n" 175 + "Verification in order failure:" 176 + "\n" 177 + "iMethods.simpleMethod(2);" 178 + "\n" 179 + "Wanted 2 times:" 180 + "\n" 181 + "-> at"; 182 assertThat(e).hasMessageContaining(expectedMessage); 183 184 String expectedCause = "\n" + "But was 1 time:" + "\n" + "-> at"; 185 186 assertThat(e).hasMessageContaining(expectedCause); 187 } 188 } 189 } 190