| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app.test; |
| 18 |
|
| 19 |
import org.mockito.invocation.InvocationOnMock; |
| 20 |
import org.mockito.stubbing.Answer; |
| 21 |
|
| 22 |
import java.lang.reflect.InvocationTargetException; |
| 23 |
import java.lang.reflect.Method; |
| 24 |
import java.util.Arrays; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class MockAnswerUtil { |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public static class AnswerWithArguments implements Answer<Object> { |
| 37 |
@Override |
| 38 |
public final Object answer(InvocationOnMock invocation) throws Throwable { |
| 39 |
Method method = invocation.getMethod(); |
| 40 |
try { |
| 41 |
Method implementation = getClass().getMethod("answer", method.getParameterTypes()); |
| 42 |
if (!implementation.getReturnType().equals(method.getReturnType())) { |
| 43 |
throw new RuntimeException("Found answer method does not have expected return " |
| 44 |
+ "type. Expected: " + method.getReturnType() + ", got " |
| 45 |
+ implementation.getReturnType()); |
| 46 |
} |
| 47 |
Object[] args = invocation.getArguments(); |
| 48 |
try { |
| 49 |
return implementation.invoke(this, args); |
| 50 |
} catch (IllegalAccessException e) { |
| 51 |
throw new RuntimeException("Error invoking answer method", e); |
| 52 |
} catch (InvocationTargetException e) { |
| 53 |
throw e.getCause(); |
| 54 |
} |
| 55 |
} catch (NoSuchMethodException e) { |
| 56 |
throw new RuntimeException("Could not find answer method with the expected args " |
| 57 |
+ Arrays.toString(method.getParameterTypes()), e); |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
} |