• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockito.internal.stubbing.defaultanswers;
6 
7 import org.mockito.internal.util.MockUtil;
8 import org.mockito.invocation.InvocationOnMock;
9 import org.mockito.stubbing.Answer;
10 
11 import java.io.Serializable;
12 
13 public class TriesToReturnSelf implements Answer<Object>, Serializable{
14 
15     private final ReturnsEmptyValues defaultReturn = new ReturnsEmptyValues();
16 
answer(InvocationOnMock invocation)17     public Object answer(InvocationOnMock invocation) throws Throwable {
18         Class<?> methodReturnType = invocation.getMethod().getReturnType();
19         Object mock = invocation.getMock();
20         Class<?> mockType = MockUtil.getMockHandler(mock).getMockSettings().getTypeToMock();
21 
22         if (methodReturnType.isAssignableFrom(mockType)) {
23             return invocation.getMock();
24         }
25 
26         return defaultReturn.returnValueFor(methodReturnType);
27     }
28 
29 }
30