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.stacktrace;
7
8import org.junit.Before;
9import org.junit.Test;
10import org.junit.runner.RunWith;
11import org.mockito.Mock;
12import org.mockito.exceptions.verification.NeverWantedButInvoked;
13import org.mockito.junit.MockitoJUnitRunner;
14import org.mockitousage.IMethods;
15import org.mockitoutil.TestBase;
16
17import static junit.framework.TestCase.fail;
18import static org.assertj.core.api.Assertions.assertThat;
19import static org.mockito.Mockito.times;
20import static org.mockito.Mockito.verify;
21
22//This is required to make sure stack trace is well filtered when runner is ON
23@RunWith(MockitoJUnitRunner.class)
24public class PointingStackTraceToActualInvocationTest extends TestBase {
25
26    @Mock private IMethods mock;
27    @Mock private IMethods mockTwo;
28
29    @Before
30    public void setup() {
31        first();
32        second();
33        third();
34        fourth();
35    }
36
37    private void first() {
38        mock.simpleMethod(1);
39    }
40    private void second() {
41        mockTwo.simpleMethod(2);
42    }
43    private void third() {
44        mock.simpleMethod(3);
45    }
46    private void fourth() {
47        mockTwo.simpleMethod(4);
48    }
49
50    @Test
51    public void shouldPointToTooManyInvocationsChunkOnError() {
52        try {
53            verify(mock, times(0)).simpleMethod(1);
54            fail();
55        } catch (NeverWantedButInvoked e) {
56            assertThat(e).hasMessageContaining("first(");
57        }
58    }
59
60    @Test
61    public void shouldNotPointStackTracesToRunnersCode() {
62        try {
63            verify(mock, times(0)).simpleMethod(1);
64            fail();
65        } catch (NeverWantedButInvoked e) {
66            assertThat(e.getMessage()).doesNotContain(".runners.");
67        }
68    }
69}
70