mock).getName())); invocationControl.verify(); return new DefaultConstructorArgumentsVerfication<T>((NewInvocationControl<T>) invocationControl, mock);
@Override public void notifyAfterTestMethod(Object testInstance, Method method, Object[] arguments, TestMethodResult testResult) { for (final PowerMockTestListener testListener : powerMockTestListeners) { try { testListener.afterTestMethod(testInstance, method, arguments, testResult); } catch (Exception e) { throw new RuntimeException(String.format(ERROR_MESSAGE_TEMPLATE, "afterTestMethod", testListener), e); } } }
@Override public void notifyAfterTestSuiteEnded(Class<?> testClass, Method[] methods, TestSuiteResult testResult) { for (PowerMockTestListener powerMockTestListener : powerMockTestListeners) { try { powerMockTestListener.afterTestSuiteEnded(testClass, methods, testResult); } catch (Exception e) { throw new RuntimeException(String.format(ERROR_MESSAGE_TEMPLATE, "afterTestSuiteEnded", powerMockTestListener), e); } } }
@SuppressWarnings({"unchecked", "rawtypes"}) private OngoingStubbing<T> createNewSubstituteMock(Class<T> type, Class<?>[] parameterTypes, Object... arguments) throws Exception { if (type == null) { throw new IllegalArgumentException("type cannot be null"); } final Class<T> unmockedType = (Class<T>) WhiteboxImpl.getOriginalUnmockedType(type); if (parameterTypes == null) { WhiteboxImpl.findUniqueConstructorOrThrowException(type, arguments); } else { WhiteboxImpl.getConstructor(unmockedType, parameterTypes); } NewInvocationControl<OngoingStubbing<T>> newInvocationControl = createNewInvocationControl(type, unmockedType); return newInvocationControl.expectSubstitutionLogic(arguments); }
/** * Note: doesn't clear PowerMock state. */ private static synchronized void verifyClass(Class<?>... types) { for (Class<?> type : types) { final MethodInvocationControl invocationHandler = MockRepository.getStaticMethodInvocationControl(type); if (invocationHandler != null) { invocationHandler.verify(); } NewInvocationControl<?> newInvocationControl = MockRepository.getNewInstanceControl(type); if (newInvocationControl != null) { try { newInvocationControl.verify(); } catch (AssertionError e) { NewInvocationControlAssertionError.throwAssertionErrorForNewSubstitutionFailure(e, type); } } } }
private static synchronized void replay(Class<?>... types) { for (Class<?> type : types) { final MethodInvocationControl invocationHandler = MockRepository.getStaticMethodInvocationControl(type); if (invocationHandler != null) { invocationHandler.replay(); } NewInvocationControl<?> newInvocationControl = MockRepository.getNewInstanceControl(type); if (newInvocationControl != null) { newInvocationControl.replay(); } } }
/** * Reset a list of class mocks. */ public static synchronized void reset(Class<?>... classMocks) { for (Class<?> type : classMocks) { final MethodInvocationControl invocationHandler = MockRepository.getStaticMethodInvocationControl(type); if (invocationHandler != null) { invocationHandler.reset(); } NewInvocationControl<?> newInvocationControl = MockRepository.getNewInstanceControl(type); if (newInvocationControl != null) { try { newInvocationControl.reset(); } catch (AssertionError e) { NewInvocationControlAssertionError.throwAssertionErrorForNewSubstitutionFailure(e, type); } } } }
/** * {@link #noMockito} is wrapped into it's own static class to make sure it is initialized not earlier than * {@link #calledFromMockito()} is called for the first time. */ private static final class NoMockito { static final boolean noMockito = Package.getPackage("org.mockito") == null; private NoMockito() {} } public static final Object PROCEED = new Object();
@Override public void notifyBeforeTestSuiteStarted(Class<?> testClass, Method[] testMethods) { for (PowerMockTestListener powerMockTestListener : powerMockTestListeners) { try { powerMockTestListener.beforeTestSuiteStarted(testClass, testMethods); } catch (Exception e) { throw new RuntimeException(String.format(ERROR_MESSAGE_TEMPLATE, "beforeTestSuiteStarted", powerMockTestListener), e); } } }
private MockPolicyClassLoadingSettings getClassLoadingSettings() { MockPolicyClassLoadingSettings settings = new MockPolicyClassLoadingSettingsImpl(); for (PowerMockPolicy mockPolicy : mockPolicies) { mockPolicy.applyClassLoadingPolicy(settings); } return settings; }
private MockPolicyInterceptionSettings getInterceptionSettings() { MockPolicyInterceptionSettings settings = new MockPolicyInterceptionSettingsImpl(); for (PowerMockPolicy mockPolicy : mockPolicies) { mockPolicy.applyInterceptionPolicy(settings); } return settings; }
@Override public void notifyBeforeTestMethod(Object testInstance, Method testMethod, Object[] arguments) { MockRepository.putAdditionalState(Keys.CURRENT_TEST_INSTANCE, testInstance); MockRepository.putAdditionalState(Keys.CURRENT_TEST_METHOD, testMethod); MockRepository.putAdditionalState(Keys.CURRENT_TEST_METHOD_ARGUMENTS, arguments); for (final PowerMockTestListener testListener : powerMockTestListeners) { try { testListener.beforeTestMethod(testInstance, testMethod, arguments); } catch (Exception e) { throw new RuntimeException(String.format(ERROR_MESSAGE_TEMPLATE, "beforeTestMethod", testListener), e); } } }
return newInvocationControl.invoke(type, args, sig);
MethodInvocationControl invocationControl = MockRepository.getInstanceMethodInvocationControl(mock); if (invocationControl != null) { invocationControl.verify(); } else { if (isNiceReplayAndVerifyMode() && !isEasyMocked(mock)) {
MethodInvocationControl invocationControl = MockRepository.getInstanceMethodInvocationControl(mock); if (invocationControl != null) { invocationControl.reset(); } else { if (isNiceReplayAndVerifyMode() && !isEasyMocked(mock)) {
MethodInvocationControl invocationControl = MockRepository.getInstanceMethodInvocationControl(mock); if (invocationControl != null) { invocationControl.replay(); } else { if (isNiceReplayAndVerifyMode() && !isEasyMocked(mock)) {
return null; return newInvocationControl.expectSubstitutionLogic(arguments);
invocationControl.verify(); } finally { MockRepository.removeAdditionalState("VerificationMode");
@SuppressWarnings({"unchecked", "rawtypes"}) private <T> OngoingStubbing<T> createNewSubstituteMock(Class<T> type, Class<?>[] parameterTypes, Object... arguments) throws Exception { if (type == null) { throw new IllegalArgumentException("type cannot be null"); } final Class<T> unmockedType = (Class<T>) WhiteboxImpl.getOriginalUnmockedType(type); if (parameterTypes == null) { WhiteboxImpl.findUniqueConstructorOrThrowException(type, arguments); } else { WhiteboxImpl.getConstructor(unmockedType, parameterTypes); } /* * Check if this type has been mocked before */ NewInvocationControl<OngoingStubbing<T>> newInvocationControl = (NewInvocationControl<OngoingStubbing<T>>) MockRepository.getNewInstanceControl(unmockedType); if (newInvocationControl == null) { InvocationSubstitute<T> mock = getMockCreator().createMock(InvocationSubstitute.class, false, false, null, null, (Method[]) null); newInvocationControl = createNewInvocationControl(mock); MockRepository.putNewInstanceControl(type, newInvocationControl); MockRepository.addObjectsToAutomaticallyReplayAndVerify(WhiteboxImpl.getOriginalUnmockedType(type)); } return newInvocationControl.expectSubstitutionLogic(arguments); }