@Override public String toString() { return new ToStringGenerator().generate(getMock(), getMethod(), getArguments()); } };
<T> NewInvocationControl<OngoingStubbing<T>> createNewInvocationControl(InvocationSubstitute<T> mock) { return new MockitoNewInvocationControl(mock); }
private boolean isInVerificationMode() { return getVerificationMode() != null; }
private void invokeSubstitute(Object... arguments) throws Exception { try { invocationControl.getSubstitute().performSubstitutionLogic(arguments); } catch (MockitoAssertionError e) { InvocationControlAssertionError.throwAssertionErrorForNewSubstitutionFailure(e, type); } }
if (hasDelegator() && !Modifier.isPrivate(methodModifiers) && !Modifier.isFinal(methodModifiers) && !Modifier.isStatic(methodModifiers) && hasBeenCaughtByMockitoProxy()) { returnValue = MockGateway.PROCEED; } else { boolean inVerificationMode = isInVerificationMode(); if (WhiteboxImpl.isClass(obj) && inVerificationMode) { handleStaticVerification((Class<?>) obj); returnValue = performIntercept(methodInterceptorFilter, obj, method, arguments); if (returnValue == null) { return MockGateway.SUPPRESS;
private Object performIntercept(MethodInterceptorFilter invocationHandler, final Object interceptionObject, final Method method, Object[] arguments) throws Throwable { MockHandler mockHandler = invocationHandler.getHandler(); final CleanTraceRealMethod cglibProxyRealMethod = new CleanTraceRealMethod(new MockedRealMethod(method, interceptionObject)); Invocation invocation = new InvocationImpl( interceptionObject, new DelegatingMethod(method), arguments, SequenceNumber.next(), cglibProxyRealMethod) { private static final long serialVersionUID = -3679957412502758558L; @Override public String toString() { return new ToStringGenerator().generate(getMock(), getMethod(), getArguments()); } }; try { return replaceMatchersBinderIfNeeded(mockHandler).handle(invocation); } catch (NotAMockException e) { if(invocation.getMock().getClass().getName().startsWith("java.") && MockRepository.getInstanceMethodInvocationControl(invocation.getMock()) != null) { return invocation.callRealMethod(); } else { throw e; } } catch (MockitoAssertionError e) { InvocationControlAssertionError.updateErrorMessageForMethodInvocation(e); throw e; } }
private static void verifyNoMoreInteractions(Class<?>... types) { for (Class<?> type : types) { final MockitoMethodInvocationControl invocationHandler = (MockitoMethodInvocationControl) MockRepository .getStaticMethodInvocationControl(type); if (invocationHandler != null) { invocationHandler.verifyNoMoreInteractions(); } MockitoNewInvocationControl<?> newInvocationControl = (MockitoNewInvocationControl<?>) MockRepository.getNewInstanceControl(type); if (newInvocationControl != null) { newInvocationControl.verifyNoMoreInteractions(); } } } }
public void verifyNoMoreInteractions() { try { final MockHandler mockHandler = methodInterceptorFilter.getHandler(); if (mockHandler instanceof MockHandler) { InvocationContainer invocationContainer = Whitebox.invokeMethod(mockHandler, "getInvocationContainer"); VerificationDataImpl data = new VerificationDataImpl(invocationContainer, null); VerificationModeFactory.noMoreInteractions().verify(data); } else { throw new RuntimeException( "Cannot perform verifyNoMoreInteractions because of unknown mockhandler type " + mockHandler.getClass()); } } catch (MockitoAssertionError e) { InvocationControlAssertionError.updateErrorMessageForVerifyNoMoreInteractions(e); throw e; } catch (Exception e) { throw new RuntimeException("PowerMock internal error",e); } }
private MockHandler replaceMatchersBinderIfNeeded(MockHandler mockHandler) { if(!Whitebox.getFieldsOfType(mockHandler, MatchersBinder.class).isEmpty()) { Whitebox.setInternalState(mockHandler, new PowerMockMatchersBinder()); } else if(!Whitebox.getFieldsOfType(mockHandler, InternalMockHandler.class).isEmpty()) { final MockHandler internalMockHandler = Whitebox.getInternalState(mockHandler, MockHandler.class); return replaceMatchersBinderIfNeeded(internalMockHandler); } return mockHandler; }
private VerificationMode getVerificationMode() { try { MockingProgress progress = Whitebox.invokeMethod(ThreadSafeMockingProgress.class, "threadSafely"); return getVerificationModeFromMockProgress(progress); } catch (Exception e) { throw new RuntimeException(e); } }
private boolean isNextCallShouldNotBeMocked(Class<?> type) { return !isFinalSystemJavaClass(type); }
public InvocationMatcher bindMatchers(ArgumentMatcherStorage argumentMatcherStorage, final Invocation invocation) { List<LocalizedMatcher> lastMatchers = argumentMatcherStorage.pullLocalizedMatchers(); validateMatchers(invocation, lastMatchers); final InvocationMatcher invocationWithMatchers = new InvocationMatcher(invocation, (List<Matcher>)(List) lastMatchers) { @Override public String toString() { return invocation.toString(); } }; return invocationWithMatchers; }
this.mockedMethods = toSet(methodsToMock); this.mockInstance = mockInstance; this.delegator = delegator;
@Override public Object invoke(Object target, Object[] arguments) throws Throwable { /* * Instruct the MockGateway to don't intercept the next call. * The reason is that when Mockito is spying on objects it * should call the "real method" (which is proxied by Mockito * anyways) so that we don't end up in here one more time which * causes infinite recursion. This should not be done if the * interceptionObject is a final system class because these are * never caught by the Mockito proxy. */ notMockNextCallIfRequired(); try { return method.invoke(target, arguments); } catch (InvocationTargetException e) { SafeExceptionRethrower.safeRethrow(e.getCause()); } return null; }
private void notMockNextCallIfRequired() { final Class<?> type = Whitebox.getType(interceptionObject); if (isNextCallShouldNotBeMocked(type)) { MockRepository.putAdditionalState(MockGateway.DONT_MOCK_NEXT_CALL, true); } }
public static void verifyNoMoreInteractions(Object... objects) { for (Object mock : objects) { if (mock instanceof Class<?>) { verifyNoMoreInteractions((Class<?>) mock); } else { MockitoMethodInvocationControl invocationControl = (MockitoMethodInvocationControl) MockRepository .getInstanceMethodInvocationControl(mock); if (invocationControl != null) { invocationControl.verifyNoMoreInteractions(); } else { /* * Delegate to Mockito if we have no handler registered for * this object. */ Mockito.verifyNoMoreInteractions(mock); } } } }
@SuppressWarnings("unchecked") private void addAnswersForStubbing(MockitoMethodInvocationControl invocationControl) { final MockHandler mockHandler = invocationControl.getInvocationHandler().getHandler(); final List list = Whitebox.getInternalState(this, List.class); try { Whitebox.invokeMethod(mockHandler, "setAnswersForStubbing", list); } catch (Exception e) { throw new RuntimeException(e); } }
mcl.cache(mock.getClass()); final MockitoMethodInvocationControl invocationControl = new MockitoMethodInvocationControl( filter, isSpy && delegator == null ? new Object() : delegator,
private void handleStaticVerification(Class<?> cls) { VerificationMode verificationMode = getVerificationMode(); if (verificationMode instanceof StaticMockAwareVerificationMode) { ((StaticMockAwareVerificationMode) verificationMode).setClassMock(cls); } }
@SuppressWarnings("unchecked") private VerificationMode getVerificationModeFromMockProgress(MockingProgress mockingProgress) { if (mockingProgress == null) { return null; } if (mockingProgress instanceof ThreadSafeMockingProgress) { ThreadLocal<MockingProgress> threadLocal = Whitebox.getInternalState(mockingProgress, ThreadLocal.class); return getVerificationModeFromMockProgress(threadLocal.get()); } else { Localized<VerificationMode> verificationMode = Whitebox.getInternalState(mockingProgress, Localized.class); return verificationMode == null ? null : verificationMode.getObject(); } }