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 org.junit.Test; 8 import org.mockito.Mock; 9 import org.mockito.invocation.InvocationOnMock; 10 import org.mockito.stubbing.Answer; 11 import org.mockitoutil.TestBase; 12 13 import static org.junit.Assert.assertEquals; 14 import static org.mockito.Mockito.when; 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()).thenAnswer(new Answer<Integer>() { 29 @Override 30 public Integer answer(InvocationOnMock invocation) throws Throwable { 31 return bar.doInt(); 32 } 33 }); 34 assertEquals(0, foo.doInt()); 35 assertEquals(0, bar.doInt()); 36 37 //when we override the stubbing 38 when(foo.doInt()).thenReturn(1); 39 40 //we expect it to be reflected: 41 assertEquals(1, foo.doInt()); 42 43 //but the stubbing on a different mock should not be altered: 44 assertEquals(0, bar.doInt()); 45 } 46 47 @Test return_type_validation()48 public void return_type_validation() throws Exception { 49 when(foo.doString()).thenAnswer(new Answer<String>() { 50 public String answer(InvocationOnMock invocation) throws Throwable { 51 //invoking a method on a different mock, with different return type 52 return String.valueOf(bar.doInt()); 53 } 54 }); 55 assertEquals("0", foo.doString()); 56 57 //we can override stubbing without misleading return type validation errors: 58 when(foo.doString()).thenReturn(""); 59 assertEquals("", foo.doString()); 60 } 61 62 @Test prevents_stack_overflow()63 public void prevents_stack_overflow() throws Exception { 64 when(foo.doInt()).thenAnswer(new Answer<Integer>() { 65 public Integer answer(InvocationOnMock invocation) throws Throwable { 66 return bar.doInt(); 67 } 68 }); 69 assertEquals(0, foo.doInt()); 70 71 when(foo.doInt()).thenAnswer(new Answer<Integer>() { 72 public Integer answer(InvocationOnMock invocation) throws Throwable { 73 return bar.doInt() + 1; 74 } 75 }); 76 77 //calling below used to cause SO error 78 assertEquals(1, foo.doInt()); 79 } 80 81 @Test overriding_stubbing()82 public void overriding_stubbing() throws Exception { 83 when(bar.doInt()).thenReturn(10); 84 when(foo.doInt()).thenAnswer(new Answer<Integer>() { 85 public Integer answer(InvocationOnMock invocation) throws Throwable { 86 return bar.doInt() + 1; 87 } 88 }); 89 90 assertEquals(11, foo.doInt()); 91 92 //when we override the stubbing with a different one 93 when(foo.doInt()).thenReturn(100); 94 95 //we expect it to be reflected: 96 assertEquals(100, foo.doInt()); 97 } 98 99 interface Foo { doString()100 String doString(); doInt()101 int doInt(); 102 } 103 104 interface Bar { doInt()105 int doInt(); 106 } 107 } 108