• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.verify;
10 
11 import org.junit.Test;
12 import org.mockito.Mock;
13 import org.mockito.exceptions.verification.WantedButNotInvoked;
14 import org.mockitousage.IMethods;
15 import org.mockitoutil.TestBase;
16 
17 public class OrdinaryVerificationPrintsAllInteractionsTest extends TestBase {
18 
19     @Mock private IMethods mock;
20     @Mock private IMethods mockTwo;
21 
22     @Test
shouldShowAllInteractionsOnMockWhenOrdinaryVerificationFail()23     public void shouldShowAllInteractionsOnMockWhenOrdinaryVerificationFail() throws Exception {
24         // given
25         firstInteraction();
26         secondInteraction();
27 
28         verify(mock).otherMethod(); // verify 1st interaction
29         try {
30             // when
31             verify(mock).simpleMethod();
32             fail();
33         } catch (WantedButNotInvoked e) {
34             // then
35             assertThat(e)
36                     .hasMessageContaining(
37                             "However, there were exactly 2 interactions with this mock")
38                     .hasMessageContaining("firstInteraction(")
39                     .hasMessageContaining("secondInteraction(");
40         }
41     }
42 
43     @Test
shouldNotShowAllInteractionsOnDifferentMock()44     public void shouldNotShowAllInteractionsOnDifferentMock() throws Exception {
45         differentMockInteraction();
46         firstInteraction();
47 
48         try {
49             verify(mock).simpleMethod();
50             fail();
51         } catch (WantedButNotInvoked e) {
52             assertThat(e.getMessage())
53                     .contains("firstInteraction(")
54                     .doesNotContain("differentMockInteraction(");
55         }
56     }
57 
58     @Test
shouldNotShowAllInteractionsHeaderWhenNoOtherInteractions()59     public void shouldNotShowAllInteractionsHeaderWhenNoOtherInteractions() throws Exception {
60         try {
61             verify(mock).simpleMethod();
62             fail();
63         } catch (WantedButNotInvoked e) {
64             assertThat(e).hasMessageContaining("there were zero interactions with this mock.");
65         }
66     }
67 
differentMockInteraction()68     private void differentMockInteraction() {
69         mockTwo.simpleMethod();
70     }
71 
secondInteraction()72     private void secondInteraction() {
73         mock.booleanReturningMethod();
74     }
75 
firstInteraction()76     private void firstInteraction() {
77         mock.otherMethod();
78     }
79 }
80