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 //This is required to make sure stack trace is well filtered when runner is ON 23 @RunWith(MockitoJUnitRunner.class) 24 public class PointingStackTraceToActualInvocationInOrderTest extends TestBase { 25 26 @Mock private IMethods mock; 27 @Mock private IMethods mockTwo; 28 private InOrder inOrder; 29 30 @Before setup()31 public void setup() { 32 inOrder = inOrder(mock, mockTwo); 33 34 first(); 35 second(); 36 third(); 37 fourth(); 38 } 39 first()40 private void first() { 41 mock.simpleMethod(1); 42 } second()43 private void second() { 44 mockTwo.simpleMethod(2); 45 } third()46 private void third() { 47 mock.simpleMethod(3); 48 } fourth()49 private void fourth() { 50 mockTwo.simpleMethod(4); 51 } 52 53 @Test shouldPointStackTraceToPreviousVerified()54 public void shouldPointStackTraceToPreviousVerified() { 55 inOrder.verify(mock, atLeastOnce()).simpleMethod(anyInt()); 56 inOrder.verify(mockTwo).simpleMethod(anyInt()); 57 58 try { 59 inOrder.verify(mock).simpleMethod(999); 60 fail(); 61 } catch (VerificationInOrderFailure e) { 62 assertThat(e).hasMessageContaining("fourth("); 63 } 64 } 65 66 @Test shouldPointToThirdMethod()67 public void shouldPointToThirdMethod() { 68 inOrder.verify(mock, atLeastOnce()).simpleMethod(anyInt()); 69 70 try { 71 inOrder.verify(mockTwo).simpleMethod(999); 72 fail(); 73 } catch (VerificationInOrderFailure e) { 74 assertThat(e).hasMessageContaining("third("); 75 } 76 } 77 78 @Test shouldPointToSecondMethod()79 public void shouldPointToSecondMethod() { 80 inOrder.verify(mock).simpleMethod(anyInt()); 81 inOrder.verify(mockTwo).simpleMethod(anyInt()); 82 83 try { 84 inOrder.verify(mockTwo, times(3)).simpleMethod(999); 85 fail(); 86 } catch (VerificationInOrderFailure e) { 87 assertThat(e).hasMessageContaining("second("); 88 } 89 } 90 91 @Test shouldPointToFirstMethodBecauseOfTooManyActualInvocations()92 public void shouldPointToFirstMethodBecauseOfTooManyActualInvocations() { 93 try { 94 inOrder.verify(mock, times(0)).simpleMethod(anyInt()); 95 fail(); 96 } catch (VerificationInOrderFailure e) { 97 assertThat(e).hasMessageContaining("first("); 98 } 99 } 100 101 @Test shouldPointToSecondMethodBecauseOfTooManyActualInvocations()102 public void shouldPointToSecondMethodBecauseOfTooManyActualInvocations() { 103 inOrder.verify(mock).simpleMethod(anyInt()); 104 105 try { 106 inOrder.verify(mockTwo, times(0)).simpleMethod(anyInt()); 107 fail(); 108 } catch (VerificationInOrderFailure e) { 109 assertThat(e).hasMessageContaining("second("); 110 } 111 } 112 113 @Test shouldPointToFourthMethodBecauseOfTooLittleActualInvocations()114 public void shouldPointToFourthMethodBecauseOfTooLittleActualInvocations() { 115 inOrder.verify(mock).simpleMethod(anyInt()); 116 inOrder.verify(mockTwo).simpleMethod(anyInt()); 117 inOrder.verify(mock).simpleMethod(anyInt()); 118 119 try { 120 inOrder.verify(mockTwo, times(3)).simpleMethod(anyInt()); 121 fail(); 122 } catch (VerificationInOrderFailure e) { 123 assertThat(e).hasMessageContaining("fourth("); 124 } 125 } 126 } 127