com.fitbur.mockito.internal.MockitoCore Maven / Gradle / Ivy
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package com.fitbur.mockito.internal;
import static com.fitbur.mockito.exceptions.Reporter.missingMethodInvocation;
import static com.fitbur.mockito.exceptions.Reporter.mocksHaveToBePassedToVerifyNoMoreInteractions;
import static com.fitbur.mockito.exceptions.Reporter.mocksHaveToBePassedWhenCreatingInOrder;
import static com.fitbur.mockito.exceptions.Reporter.notAMockPassedToVerify;
import static com.fitbur.mockito.exceptions.Reporter.notAMockPassedToVerifyNoMoreInteractions;
import static com.fitbur.mockito.exceptions.Reporter.notAMockPassedWhenCreatingInOrder;
import static com.fitbur.mockito.exceptions.Reporter.nullPassedToVerify;
import static com.fitbur.mockito.exceptions.Reporter.nullPassedToVerifyNoMoreInteractions;
import static com.fitbur.mockito.exceptions.Reporter.nullPassedWhenCreatingInOrder;
import static com.fitbur.mockito.internal.verification.VerificationModeFactory.noMoreInteractions;
import java.util.Arrays;
import java.util.List;
import com.fitbur.mockito.InOrder;
import com.fitbur.mockito.MockSettings;
import com.fitbur.mockito.MockingDetails;
import com.fitbur.mockito.exceptions.misusing.NotAMockException;
import com.fitbur.mockito.internal.creation.MockSettingsImpl;
import com.fitbur.mockito.internal.invocation.finder.VerifiableInvocationsFinder;
import com.fitbur.mockito.internal.progress.MockingProgress;
import com.fitbur.mockito.internal.progress.ThreadSafeMockingProgress;
import com.fitbur.mockito.internal.stubbing.InvocationContainer;
import com.fitbur.mockito.internal.stubbing.OngoingStubbingImpl;
import com.fitbur.mockito.internal.stubbing.StubberImpl;
import com.fitbur.mockito.internal.util.DefaultMockingDetails;
import com.fitbur.mockito.internal.util.MockUtil;
import com.fitbur.mockito.internal.verification.MockAwareVerificationMode;
import com.fitbur.mockito.internal.verification.VerificationDataImpl;
import com.fitbur.mockito.internal.verification.VerificationModeFactory;
import com.fitbur.mockito.internal.verification.api.InOrderContext;
import com.fitbur.mockito.internal.verification.api.VerificationDataInOrder;
import com.fitbur.mockito.internal.verification.api.VerificationDataInOrderImpl;
import com.fitbur.mockito.invocation.Invocation;
import com.fitbur.mockito.mock.MockCreationSettings;
import com.fitbur.mockito.stubbing.OngoingStubbing;
import com.fitbur.mockito.stubbing.Stubber;
import com.fitbur.mockito.verification.VerificationMode;
@SuppressWarnings("unchecked")
public class MockitoCore {
private final MockUtil mockUtil = new MockUtil();
private final MockingProgress mockingProgress = new ThreadSafeMockingProgress();
public boolean isTypeMockable(Class> typeToMock) {
return mockUtil.typeMockabilityOf(typeToMock).mockable();
}
public T mock(Class typeToMock, MockSettings settings) {
if (!MockSettingsImpl.class.isInstance(settings)) {
throw new IllegalArgumentException(
"Unexpected implementation of '" + settings.getClass().getCanonicalName() + "'\n"
+ "At the moment, you cannot provide your own implementations of that class.");
}
MockSettingsImpl impl = MockSettingsImpl.class.cast(settings);
MockCreationSettings creationSettings = impl.confirm(typeToMock);
T mock = mockUtil.createMock(creationSettings);
mockingProgress.mockingStarted(mock, typeToMock);
return mock;
}
public OngoingStubbing when(T methodCall) {
mockingProgress.stubbingStarted();
@SuppressWarnings("unchecked")
OngoingStubbing stubbing = (OngoingStubbing) mockingProgress.pullOngoingStubbing();
if (stubbing == null) {
mockingProgress.reset();
throw missingMethodInvocation();
}
return stubbing;
}
public T verify(T mock, VerificationMode mode) {
if (mock == null) {
throw nullPassedToVerify();
}
if (!mockUtil.isMock(mock)) {
throw notAMockPassedToVerify(mock.getClass());
}
VerificationMode actualMode = this.mockingProgress.maybeVerifyLazily(mode);
mockingProgress.verificationStarted(new MockAwareVerificationMode(mock, actualMode));
return mock;
}
public void reset(T ... mocks) {
mockingProgress.validateState();
mockingProgress.reset();
mockingProgress.resetOngoingStubbing();
for (T m : mocks) {
mockUtil.resetMock(m);
}
}
public void clearInvocations(T ... mocks) {
mockingProgress.validateState();
mockingProgress.reset();
mockingProgress.resetOngoingStubbing();
for (T m : mocks) {
mockUtil.getMockHandler(m).getInvocationContainer().clearInvocations();
}
}
public void verifyNoMoreInteractions(Object... mocks) {
assertMocksNotEmpty(mocks);
mockingProgress.validateState();
for (Object mock : mocks) {
try {
if (mock == null) {
throw nullPassedToVerifyNoMoreInteractions();
}
InvocationContainer invocations = mockUtil.getMockHandler(mock).getInvocationContainer();
VerificationDataImpl data = new VerificationDataImpl(invocations, null);
noMoreInteractions().verify(data);
} catch (NotAMockException e) {
throw notAMockPassedToVerifyNoMoreInteractions();
}
}
}
public void verifyNoMoreInteractionsInOrder(List
© 2015 - 2024 Weber Informatics LLC | Privacy Policy