org.mockito.internal.MockitoCore Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mockito-all Show documentation
Show all versions of mockito-all Show documentation
Mock objects library for java
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal;
import org.mockito.InOrder;
import org.mockito.MockSettings;
import org.mockito.exceptions.Reporter;
import org.mockito.exceptions.misusing.NotAMockException;
import org.mockito.internal.creation.MockSettingsImpl;
import org.mockito.internal.invocation.Invocation;
import org.mockito.internal.invocation.finder.VerifiableInvocationsFinder;
import org.mockito.internal.progress.IOngoingStubbing;
import org.mockito.internal.progress.MockingProgress;
import org.mockito.internal.progress.ThreadSafeMockingProgress;
import org.mockito.internal.stubbing.InvocationContainer;
import org.mockito.internal.stubbing.OngoingStubbingImpl;
import org.mockito.internal.stubbing.StubberImpl;
import org.mockito.internal.util.MockUtil;
import org.mockito.internal.verification.MockAwareVerificationMode;
import org.mockito.internal.verification.VerificationDataImpl;
import org.mockito.internal.verification.VerificationModeFactory;
import org.mockito.internal.verification.api.InOrderContext;
import org.mockito.internal.verification.api.VerificationDataInOrder;
import org.mockito.internal.verification.api.VerificationDataInOrderImpl;
import org.mockito.stubbing.*;
import org.mockito.verification.VerificationMode;
import java.util.Arrays;
import java.util.List;
@SuppressWarnings("unchecked")
public class MockitoCore {
private final Reporter reporter = new Reporter();
private final MockUtil mockUtil = new MockUtil();
private final MockingProgress mockingProgress = new ThreadSafeMockingProgress();
public T mock(Class classToMock, MockSettings mockSettings) {
T mock = mockUtil.createMock(classToMock, (MockSettingsImpl) mockSettings);
mockingProgress.mockingStarted(mock, classToMock, mockSettings);
return mock;
}
public IOngoingStubbing stub() {
IOngoingStubbing stubbing = mockingProgress.pullOngoingStubbing();
if (stubbing == null) {
mockingProgress.reset();
reporter.missingMethodInvocation();
}
return stubbing;
}
public DeprecatedOngoingStubbing stub(T methodCall) {
mockingProgress.stubbingStarted();
return (DeprecatedOngoingStubbing) stub();
}
public OngoingStubbing when(T methodCall) {
mockingProgress.stubbingStarted();
return (OngoingStubbing) stub();
}
public T verify(T mock, VerificationMode mode) {
if (mock == null) {
reporter.nullPassedToVerify();
} else if (!mockUtil.isMock(mock)) {
reporter.notAMockPassedToVerify(mock.getClass());
}
mockingProgress.verificationStarted(new MockAwareVerificationMode(mock, mode));
return mock;
}
public void reset(T ... mocks) {
mockingProgress.validateState();
mockingProgress.reset();
mockingProgress.resetOngoingStubbing();
for (T m : mocks) {
mockUtil.resetMock(m);
}
}
public void verifyNoMoreInteractions(Object... mocks) {
assertMocksNotEmpty(mocks);
mockingProgress.validateState();
for (Object mock : mocks) {
try {
if (mock == null) {
reporter.nullPassedToVerifyNoMoreInteractions();
}
InvocationContainer invocations = mockUtil.getMockHandler(mock).getInvocationContainer();
VerificationDataImpl data = new VerificationDataImpl(invocations, null);
VerificationModeFactory.noMoreInteractions().verify(data);
} catch (NotAMockException e) {
reporter.notAMockPassedToVerifyNoMoreInteractions();
}
}
}
public void verifyNoMoreInteractionsInOrder(List