/** * Spy on objects that are final or otherwise not "spyable" from * normal Mockito. * * @see Mockito#spy(Object) * * @param <T> * the type of the mock object * @param object * the object to spy on * @return the spy object. */ @SuppressWarnings("unchecked") public static synchronized <T> T spy(T object) { return DefaultMockCreator.mock((Class<T>) Whitebox.getType(object), false, true, object, null, (Method[]) null); }
private void notMockNextCallIfRequired() { final Class<?> type = Whitebox.getType(interceptionObject); if (isNextCallShouldNotBeMocked(type)) { MockRepository.putAdditionalState(MockGateway.DONT_MOCK_NEXT_CALL, true); } }
throw new IllegalStateException(String.format(NO_OBJECT_CREATION_ERROR_MESSAGE_TEMPLATE, Whitebox.getType( mock).getName()));
mock, mode)); if (invocationControl == null) { throw new IllegalStateException(String.format(NO_OBJECT_CREATION_ERROR_MESSAGE_TEMPLATE, Whitebox.getType( mock).getName()));
/** * Used to specify expectations on methods using the method name. Works on * for example private or package private methods. */ public static synchronized <T> IExpectationSetters<T> expectPrivate(Object instance, String methodName, Object... arguments) throws Exception { if (instance == null) { throw new IllegalArgumentException("Instance or class cannot be null."); } return expectPrivate(instance, methodName, Whitebox.getType(instance), arguments); }
/** * Used to specify expectations on methods without specifying a method name. * Works on for example private or package private methods. PowerMock tries * to find a unique method to expect based on the argument parameters. If * PowerMock is unable to locate a unique method you need to revert to using * {@link #expectPrivate(Object, String, Object...)}. */ public static synchronized <T> IExpectationSetters<T> expectPrivate(Object instance, Object... arguments) throws Exception { return expectPrivate(instance, null, Whitebox.getType(instance), arguments); }
@Override public Object call() throws Exception { final Class<?> type = Whitebox.getType(delegator); final boolean isFinalSystemClass = type.getName().startsWith("java.") && Modifier.isFinal(type.getModifiers()); if (!isFinalSystemClass) { MockRepository.putAdditionalState(MockGateway.DONT_MOCK_NEXT_CALL, true); } try { return method.invoke(delegator, arguments); } catch (InvocationTargetException e) { SafeExceptionRethrower.safeRethrow(e.getCause()); } return null; } };
public <T> DefaultConstructorArgumentsVerification<T> verifyNew(final Class<T> mock, final VerificationMode mode) { assertNotNull(mock, "Class to verify cannot be null"); assertNotNull(mode, "Verify mode cannot be null"); @SuppressWarnings("unchecked") MockitoNewInvocationControl<T> invocationControl = (MockitoNewInvocationControl<T>) MockRepository.getNewInstanceControl(mock); assertNotNull(invocationControl, String.format(NO_OBJECT_CREATION_ERROR_MESSAGE_TEMPLATE, Whitebox.getType(mock).getName())); invocationControl.verify(mode); //noinspection unchecked return new DefaultConstructorArgumentsVerification<T>((NewInvocationControl<T>) invocationControl, mock); }
public <T> T spy(final T object) { MockSettings mockSettings = Mockito.withSettings() .spiedInstance(object) .defaultAnswer(POWER_MOCK_CALL_REAL_METHOD); //noinspection unchecked return DefaultMockCreator.mock((Class<T>) Whitebox.getType(object), false, true, object, mockSettings, (Method[]) null); }