• 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 
6 package org.mockitousage.verification;
7 
8 import org.junit.Test;
9 import org.mockito.Mock;
10 import org.mockito.exceptions.verification.WantedButNotInvoked;
11 import org.mockitousage.IMethods;
12 import org.mockitoutil.TestBase;
13 
14 import static junit.framework.TestCase.fail;
15 import static org.assertj.core.api.Assertions.assertThat;
16 import static org.mockito.Mockito.verify;
17 
18 public class OrdinaryVerificationPrintsAllInteractionsTest extends TestBase {
19 
20     @Mock private IMethods mock;
21     @Mock private IMethods mockTwo;
22 
23     @Test
shouldShowAllInteractionsOnMockWhenOrdinaryVerificationFail()24     public void shouldShowAllInteractionsOnMockWhenOrdinaryVerificationFail() throws Exception {
25         //given
26         firstInteraction();
27         secondInteraction();
28 
29         verify(mock).otherMethod(); //verify 1st interaction
30         try {
31             //when
32             verify(mock).simpleMethod();
33             fail();
34         } catch (WantedButNotInvoked e) {
35             //then
36             assertThat(e)
37                 .hasMessageContaining("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()).contains("firstInteraction(").doesNotContain("differentMockInteraction(");
53         }
54     }
55 
56     @Test
shouldNotShowAllInteractionsHeaderWhenNoOtherInteractions()57     public void shouldNotShowAllInteractionsHeaderWhenNoOtherInteractions() throws Exception {
58         try {
59             verify(mock).simpleMethod();
60             fail();
61         } catch (WantedButNotInvoked e) {
62             assertThat(e).hasMessageContaining("there were zero interactions with this mock.");
63         }
64     }
65 
differentMockInteraction()66     private void differentMockInteraction() {
67         mockTwo.simpleMethod();
68     }
69 
secondInteraction()70     private void secondInteraction() {
71         mock.booleanReturningMethod();
72     }
73 
firstInteraction()74     private void firstInteraction() {
75         mock.otherMethod();
76     }
77 }
78