1/*
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5
6package org.mockitousage.verification;
7
8import org.junit.Test;
9import org.mockito.Mock;
10import org.mockito.exceptions.verification.WantedButNotInvoked;
11import org.mockitousage.IMethods;
12import org.mockitoutil.TestBase;
13
14import static org.junit.Assert.fail;
15import static org.assertj.core.api.Assertions.assertThat;
16import static org.mockito.Mockito.verify;
17
18public class OrdinaryVerificationPrintsAllInteractionsTest extends TestBase {
19
20    @Mock private IMethods mock;
21    @Mock private IMethods mockTwo;
22
23    @Test
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
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
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
66    private void differentMockInteraction() {
67        mockTwo.simpleMethod();
68    }
69
70    private void secondInteraction() {
71        mock.booleanReturningMethod();
72    }
73
74    private void firstInteraction() {
75        mock.otherMethod();
76    }
77}
78