• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2018 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockitousage.bugs;
6 
7 import static org.junit.Assert.assertEquals;
8 import static org.mockito.Mockito.when;
9 
10 import org.junit.Test;
11 import org.mockito.Mock;
12 import org.mockito.invocation.InvocationOnMock;
13 import org.mockito.stubbing.Answer;
14 import org.mockitoutil.TestBase;
15 
16 /**
17  * @see <a href="https://github.com/mockito/mockito/issues/1279">Issue #1279</a>
18  */
19 public class MockitoStubbedCallInAnswerTest extends TestBase {
20 
21     @Mock Foo foo;
22     @Mock Bar bar;
23 
24     @Test
stubbing_the_right_mock()25     public void stubbing_the_right_mock() throws Exception {
26         // stubbing on different mock should not be altered
27         when(bar.doInt()).thenReturn(0);
28         when(foo.doInt())
29                 .thenAnswer(
30                         new Answer<Integer>() {
31                             @Override
32                             public Integer answer(InvocationOnMock invocation) throws Throwable {
33                                 return bar.doInt();
34                             }
35                         });
36         assertEquals(0, foo.doInt());
37         assertEquals(0, bar.doInt());
38 
39         // when we override the stubbing
40         when(foo.doInt()).thenReturn(1);
41 
42         // we expect it to be reflected:
43         assertEquals(1, foo.doInt());
44 
45         // but the stubbing on a different mock should not be altered:
46         assertEquals(0, bar.doInt());
47     }
48 
49     @Test
return_type_validation()50     public void return_type_validation() throws Exception {
51         when(foo.doString())
52                 .thenAnswer(
53                         new Answer<String>() {
54                             public String answer(InvocationOnMock invocation) throws Throwable {
55                                 // invoking a method on a different mock, with different return type
56                                 return String.valueOf(bar.doInt());
57                             }
58                         });
59         assertEquals("0", foo.doString());
60 
61         // we can override stubbing without misleading return type validation errors:
62         when(foo.doString()).thenReturn("");
63         assertEquals("", foo.doString());
64     }
65 
66     @Test
prevents_stack_overflow()67     public void prevents_stack_overflow() throws Exception {
68         when(foo.doInt())
69                 .thenAnswer(
70                         new Answer<Integer>() {
71                             public Integer answer(InvocationOnMock invocation) throws Throwable {
72                                 return bar.doInt();
73                             }
74                         });
75         assertEquals(0, foo.doInt());
76 
77         when(foo.doInt())
78                 .thenAnswer(
79                         new Answer<Integer>() {
80                             public Integer answer(InvocationOnMock invocation) throws Throwable {
81                                 return bar.doInt() + 1;
82                             }
83                         });
84 
85         // calling below used to cause SO error
86         assertEquals(1, foo.doInt());
87     }
88 
89     @Test
overriding_stubbing()90     public void overriding_stubbing() throws Exception {
91         when(bar.doInt()).thenReturn(10);
92         when(foo.doInt())
93                 .thenAnswer(
94                         new Answer<Integer>() {
95                             public Integer answer(InvocationOnMock invocation) throws Throwable {
96                                 return bar.doInt() + 1;
97                             }
98                         });
99 
100         assertEquals(11, foo.doInt());
101 
102         // when we override the stubbing with a different one
103         when(foo.doInt()).thenReturn(100);
104 
105         // we expect it to be reflected:
106         assertEquals(100, foo.doInt());
107     }
108 
109     interface Foo {
doString()110         String doString();
111 
doInt()112         int doInt();
113     }
114 
115     interface Bar {
doInt()116         int doInt();
117     }
118 }
119