@Override public void when(Class<?> classMock) { MockitoMethodInvocationControl invocationControl = (MockitoMethodInvocationControl) MockRepository .getStaticMethodInvocationControl(classMock); addAnswersForStubbing(invocationControl); }
private <T> void prepareForStubbing(T mock) { MockitoMethodInvocationControl invocationControl = (MockitoMethodInvocationControl) MockRepository.getInstanceMethodInvocationControl(mock); addAnswersForStubbing(invocationControl); }
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(); } } } }
MockRepository.addAfterMethodRunner(new MockitoStateCleanerRunnable()); typeToMock = (Class<T>) new ClassReplicaCreator().createClassReplica(type); } else { typeToMock = type; if (isFinalJavaSystemClass(type) && !isStatic) { mock = Whitebox.newInstance(type); DefaultFieldValueGenerator.fillWithDefaultValues(mock); MockRepository.putStaticMethodInvocationControl(type, mockData.getMethodInvocationControl()); } else { MockRepository.putInstanceMethodInvocationControl(mock, mockData.getMethodInvocationControl());
throw new IllegalArgumentException("Verify mode cannot be null"); NewInvocationControl<?> invocationControl = MockRepository.getNewInstanceControl(mock); MockRepository.putAdditionalState("VerificationMode", POWERMOCKITO_CORE.wrapInMockitoSpecificVerificationMode( mock, mode)); if (invocationControl == null) { invocationControl.verify(); } finally { MockRepository.removeAdditionalState("VerificationMode");
throw new IllegalArgumentException("Class to verify cannot be null"); NewInvocationControl<?> invocationControl = MockRepository.getNewInstanceControl(mock); if (invocationControl == null) { throw new IllegalStateException(String.format(NO_OBJECT_CREATION_ERROR_MESSAGE_TEMPLATE, Whitebox.getType( mock).getName())); invocationControl.verify(); return new DefaultConstructorArgumentsVerfication<T>((NewInvocationControl<T>) invocationControl, mock);
private <T> void putMethodInvocationControlToRepository(final Class<T> type, final boolean isStatic, final MockData<T> mockData, final T mock) { if (isStatic) { MockRepository.putStaticMethodInvocationControl(type, mockData.getMethodInvocationControl()); } else { MockRepository.putInstanceMethodInvocationControl(mock, mockData.getMethodInvocationControl()); } }
private void notMockNextCallIfRequired() { final Class<?> type = Whitebox.getType(interceptionObject); if (isNextCallShouldNotBeMocked(type)) { MockRepository.putAdditionalState(MockGateway.DONT_MOCK_NEXT_CALL, true); } }
private static boolean isNiceReplayAndVerifyMode() { final Boolean mode = (Boolean) MockRepository.getAdditionalState(NICE_REPLAY_AND_VERIFY_KEY); return mode != null && mode; } }
/** * Suppress specific method calls on all types containing this method. This * works on both instance methods and static methods. Note that replay and * verify are not needed as this is not part of a mock behavior. */ public static synchronized void suppressMethod(Method... methods) { for (Method method : methods) { MockRepository.addMethodToSuppress(method); } }
private void registerAfterMethodRunner() { MockRepository.addAfterMethodRunner(new Runnable() { @Override public void run() { Mockito.reset(); } }); }
private boolean shouldIgnore(String[] packages, String name) { for (String ignore : packages) { if (WildcardMatcher.matches(name, ignore)) { return true; } } return false; }
@SuppressWarnings("UnusedDeclaration") public static Object methodCall(Object instance, String methodName, Object[] args, Class<?>[] sig, String returnTypeAsString) throws Throwable { return doMethodCall(instance, methodName, args, sig, returnTypeAsString); }
MockInvocation(Object object, String methodName, Class<?>... sig) { this.object = object; this.methodName = methodName; this.sig = sig; init(); }
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); } } } }
public static boolean matchesAny(Collection<String> patterns, String text) { for (String pattern : patterns) { if (matches(text, pattern)) { return true; } } return false; }
@SuppressWarnings("UnusedDeclaration") public static Object methodCall(Class<?> type, String methodName, Object[] args, Class<?>[] sig, String returnTypeAsString) throws Throwable { return doMethodCall(type, methodName, args, sig, returnTypeAsString); }
/** * Supports PowerMockito mocks. If {@code mock} is not a PowerMockito * mock it will delegate to Mockito. * * @see Stubber#when(Object) */ @Override public <T> T when(T instanceMock) { MockitoMethodInvocationControl invocationControl = (MockitoMethodInvocationControl) MockRepository .getInstanceMethodInvocationControl(instanceMock); final T returnValue; if (invocationControl == null) { returnValue = super.when(instanceMock); } else { addAnswersForStubbing(invocationControl); returnValue = instanceMock; } return returnValue; }
public static boolean matchesAny(String[] patterns, String text) { for (String string : patterns) { if (matches(text, string)) { return true; } } return false; } }
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; } }