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.stacktrace;
7 
8 import org.junit.Before;
9 import org.junit.Test;
10 import org.junit.runner.RunWith;
11 import org.mockito.InOrder;
12 import org.mockito.Mock;
13 import org.mockito.exceptions.verification.VerificationInOrderFailure;
14 import org.mockito.junit.MockitoJUnitRunner;
15 import org.mockitousage.IMethods;
16 import org.mockitoutil.TestBase;
17 
18 import static org.junit.Assert.fail;
19 import static org.assertj.core.api.Assertions.assertThat;
20 import static org.mockito.Mockito.*;
21 
22 @RunWith(MockitoJUnitRunner.class)
23 public class PointingStackTraceToActualInvocationChunkInOrderTest extends TestBase {
24 
25     @Mock private IMethods mock;
26     @Mock private IMethods mockTwo;
27     private InOrder inOrder;
28 
29     @Before
setup()30     public void setup() {
31         inOrder = inOrder(mock, mockTwo);
32 
33         firstChunk();
34         secondChunk();
35         thirdChunk();
36         fourthChunk();
37     }
38 
firstChunk()39     private void firstChunk() {
40         mock.simpleMethod(1);
41         mock.simpleMethod(1);
42     }
secondChunk()43     private void secondChunk() {
44         mockTwo.simpleMethod(2);
45         mockTwo.simpleMethod(2);
46     }
thirdChunk()47     private void thirdChunk() {
48         mock.simpleMethod(3);
49         mock.simpleMethod(3);
50     }
fourthChunk()51     private void fourthChunk() {
52         mockTwo.simpleMethod(4);
53         mockTwo.simpleMethod(4);
54     }
55 
56     @Test
shouldPointStackTraceToPreviousInvocation()57     public void shouldPointStackTraceToPreviousInvocation() {
58         inOrder.verify(mock, times(2)).simpleMethod(anyInt());
59         inOrder.verify(mockTwo, times(2)).simpleMethod(anyInt());
60 
61         try {
62             inOrder.verify(mock).simpleMethod(999);
63             fail();
64         } catch (VerificationInOrderFailure e) {
65             assertThat(e).hasMessageContaining("secondChunk(");
66         }
67     }
68 
69     @Test
shouldPointToThirdInteractionBecauseAtLeastOnceUsed()70     public void shouldPointToThirdInteractionBecauseAtLeastOnceUsed() {
71         inOrder.verify(mock, atLeastOnce()).simpleMethod(anyInt());
72 
73         try {
74             inOrder.verify(mockTwo).simpleMethod(999);
75             fail();
76         } catch (VerificationInOrderFailure e) {
77             assertThat(e).hasMessageContaining("thirdChunk(");
78         }
79     }
80 
81     @Test
shouldPointToThirdChunkWhenTooLittleActualInvocations()82     public void shouldPointToThirdChunkWhenTooLittleActualInvocations() {
83         inOrder.verify(mock, times(2)).simpleMethod(anyInt());
84         inOrder.verify(mockTwo, times(2)).simpleMethod(anyInt());
85         inOrder.verify(mock, atLeastOnce()).simpleMethod(anyInt());
86 
87         try {
88             inOrder.verify(mockTwo, times(3)).simpleMethod(999);
89             fail();
90         } catch (VerificationInOrderFailure e) {
91             assertThat(e).hasMessageContaining("thirdChunk(");
92         }
93     }
94 
95     @Test
shouldPointToFourthChunkBecauseTooManyActualInvocations()96     public void shouldPointToFourthChunkBecauseTooManyActualInvocations() {
97         inOrder.verify(mock, atLeastOnce()).simpleMethod(anyInt());
98 
99         try {
100             inOrder.verify(mockTwo, times(0)).simpleMethod(anyInt());
101             fail();
102         } catch (VerificationInOrderFailure e) {
103             assertThat(e).hasMessageContaining("fourthChunk(");
104         }
105     }
106 }
107