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(); } } } }
private <T> void prepareForStubbing(T mock) { MockitoMethodInvocationControl invocationControl = (MockitoMethodInvocationControl) MockRepository.getInstanceMethodInvocationControl(mock); addAnswersForStubbing(invocationControl); }
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");
MockRepository.addAfterMethodRunner(new MockitoStateCleanerRunnable()); MockRepository.putStaticMethodInvocationControl(type, mockData.getMethodInvocationControl()); } else { MockRepository.putInstanceMethodInvocationControl(mock, mockData.getMethodInvocationControl());
private NewInvocationControl<OngoingStubbing<T>> createNewInvocationControl(final Class<T> type, final Class<T> unmockedType) { /* * Check if this type has been mocked before */ NewInvocationControl<OngoingStubbing<T>> newInvocationControl = (NewInvocationControl<OngoingStubbing<T>>) MockRepository.getNewInstanceControl(unmockedType); if (newInvocationControl == null) { newInvocationControl = createNewInvocationControl(mock); MockRepository.putNewInstanceControl(type, newInvocationControl); MockRepository.addObjectsToAutomaticallyReplayAndVerify(WhiteboxImpl.getOriginalUnmockedType(type)); } return newInvocationControl; }
@BeforeClass protected void beforePowerMockTestClass() throws Exception { // To make sure that the mock repository is not in an incorrect state when the test begins MockRepository.clear(); if(isLoadedByPowerMockClassloader()) { final Thread thread = Thread.currentThread(); previousCl = thread.getContextClassLoader(); thread.setContextClassLoader(this.getClass().getClassLoader()); } }
@SuppressWarnings("unused") private void initializeInterceptionSettings() { MockPolicyInterceptionSettings interceptionSettings = getInterceptionSettings(); for (Method method : interceptionSettings.getMethodsToSuppress()) { MockRepository.addMethodToSuppress(method); } for (Entry<Method, InvocationHandler> entry : interceptionSettings.getProxiedMethods().entrySet()) { MockRepository.putMethodProxy(entry.getKey(), entry.getValue()); } for (Entry<Method, Object> entry : interceptionSettings.getStubbedMethods().entrySet()) { final Method method = entry.getKey(); final Object className = entry.getValue(); MockRepository.putMethodToStub(method, className); } for (Field field : interceptionSettings.getFieldsToSuppress()) { MockRepository.addFieldToSuppress(field); } for (String type : interceptionSettings.getFieldTypesToSuppress()) { MockRepository.addFieldTypeToSuppress(type); } }
reset((Class<?>) mock); } else { MethodInvocationControl invocationControl = MockRepository.getInstanceMethodInvocationControl(mock); if (invocationControl != null) { invocationControl.reset(); MockRepository.putAdditionalState(NICE_REPLAY_AND_VERIFY_KEY, false); if (t instanceof RuntimeException) { throw (RuntimeException) t;
/** * 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); } }
final Set<Method> methodsToMock = toSet(methods); if (isStatic) { MockRepository.putStaticMethodInvocationControl(type, new EasyMockMethodInvocationControl<Object>(h, methodsToMock, replica)); MockRepository.addObjectsToAutomaticallyReplayAndVerify(type); return null; } else { T newInstance = Whitebox.newInstance(type); MockRepository .putInstanceMethodInvocationControl(newInstance, new EasyMockMethodInvocationControl<Object>(h, methodsToMock, replica)); if (newInstance instanceof InvocationSubstitute<?> == false) { MockRepository.addObjectsToAutomaticallyReplayAndVerify(newInstance); final Set<Method> methodsToMock = toSet(methods); if (isStatic) { MockRepository.putStaticMethodInvocationControl(type, new EasyMockMethodInvocationControl<T>(h, methodsToMock, mock)); MockRepository.addObjectsToAutomaticallyReplayAndVerify(type); } else { MockRepository.putInstanceMethodInvocationControl(mock, new EasyMockMethodInvocationControl<T>(h, methodsToMock)); if (mock instanceof InvocationSubstitute<?> == false) { MockRepository.addObjectsToAutomaticallyReplayAndVerify(mock);
private void init() { if (object instanceof Class<?>) { objectType = (Class<?>) object; methodInvocationControl = MockRepository.getStaticMethodInvocationControl(objectType); } else { final Class<?> type = object.getClass(); UnproxiedType unproxiedType = WhiteboxImpl.getUnproxiedType(type); objectType = unproxiedType.getOriginalType(); methodInvocationControl = MockRepository.getInstanceMethodInvocationControl(object); } method = findMethodToInvoke(methodName, sig, objectType); }
private void notMockNextCallIfRequired() { final Class<?> type = Whitebox.getType(interceptionObject); if (isNextCallShouldNotBeMocked(type)) { MockRepository.putAdditionalState(MockGateway.DONT_MOCK_NEXT_CALL, true); } }
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(
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()); } }
/** * Suppress specific fields. 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 suppressField(Field... fields) { for (Field field : fields) { MockRepository.addFieldToSuppress(field); } }
@SuppressWarnings("UnusedDeclaration") public static Object newInstanceCall(Class<?> type, Object[] args, Class<?>[] sig) throws Throwable { final NewInvocationControl<?> newInvocationControl = MockRepository.getNewInstanceControl(type); if (newInvocationControl != null) { if (MockRepository.shouldSuppressConstructor(WhiteboxImpl.getConstructor(type, sig))) { return WhiteboxImpl.getFirstParentConstructor(type);
@Override public void when(Class<?> classMock) { MockitoMethodInvocationControl invocationControl = (MockitoMethodInvocationControl) MockRepository .getStaticMethodInvocationControl(classMock); addAnswersForStubbing(invocationControl); }
/** * Reset all classes and mock objects known by PowerMock. This includes all * classes that are prepared for test using the {@link PrepareForTest} or * {@link PrepareOnlyThisForTest} annotations and all classes that have had * their static initializers removed by using the * {@link SuppressStaticInitializationFor} annotation. It also includes all * mock instances created by PowerMock such as those created or used by * {@link #createMock(Class, Method...)}, * {@link #mockStatic(Class, Method...)}, * {@link #expectNew(Class, Object...)}, * {@link #createPartialMock(Class, String...)} etc. * <p> * To make it easy to pass in additional mocks <i>not</i> created by the * PowerMock API you can optionally specify them as <tt>additionalMocks</tt> * . These are typically those mock objects you have created using pure * EasyMock or EasyMock class extensions. No additional mocks needs to be * specified if you're only using PowerMock API methods. * * @param additionalMocks * Mocks not created by the PowerMock API. These are typically * those mock objects you have created using pure EasyMock or * EasyMock class extensions. */ public static synchronized void resetAll(Object... additionalMocks) { MockRepository.addObjectsToAutomaticallyReplayAndVerify(additionalMocks); for (Object classToReplayOrVerify : MockRepository.getObjectsToAutomaticallyReplayAndVerify()) { reset(classToReplayOrVerify); } }
private static boolean isNiceReplayAndVerifyMode() { final Boolean mode = (Boolean) MockRepository.getAdditionalState(NICE_REPLAY_AND_VERIFY_KEY); return mode != null && mode; } }
private static boolean shouldMockThisCall() { Object shouldSkipMockingOfNextCall = MockRepository.getAdditionalState(DONT_MOCK_NEXT_CALL); final boolean shouldMockThisCall = shouldSkipMockingOfNextCall == null; MockRepository.removeAdditionalState(DONT_MOCK_NEXT_CALL); return shouldMockThisCall; }