private boolean isInVerificationMode() { return getVerificationMode() != null; }
private VerificationMode getVerificationMode() { try { MockingProgress progress = Whitebox.invokeMethod(ThreadSafeMockingProgress.class, "threadSafely"); return getVerificationModeFromMockProgress(progress); } catch (Exception e) { throw new RuntimeException(e); } }
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 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; }
@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 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; } }
@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(); } }
private void handleStaticVerification(Class<?> cls) { VerificationMode verificationMode = getVerificationMode(); if (verificationMode instanceof StaticMockAwareVerificationMode) { ((StaticMockAwareVerificationMode) verificationMode).setClassMock(cls); } }