• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockito.internal;
6 
7 import static org.mockito.internal.exceptions.Reporter.missingMethodInvocation;
8 import static org.mockito.internal.exceptions.Reporter.mocksHaveToBePassedToVerifyNoMoreInteractions;
9 import static org.mockito.internal.exceptions.Reporter.mocksHaveToBePassedWhenCreatingInOrder;
10 import static org.mockito.internal.exceptions.Reporter.notAMockPassedToVerify;
11 import static org.mockito.internal.exceptions.Reporter.notAMockPassedToVerifyNoMoreInteractions;
12 import static org.mockito.internal.exceptions.Reporter.notAMockPassedWhenCreatingInOrder;
13 import static org.mockito.internal.exceptions.Reporter.nullPassedToVerify;
14 import static org.mockito.internal.exceptions.Reporter.nullPassedToVerifyNoMoreInteractions;
15 import static org.mockito.internal.exceptions.Reporter.nullPassedWhenCreatingInOrder;
16 import static org.mockito.internal.progress.ThreadSafeMockingProgress.mockingProgress;
17 import static org.mockito.internal.util.MockUtil.createMock;
18 import static org.mockito.internal.util.MockUtil.getMockHandler;
19 import static org.mockito.internal.util.MockUtil.isMock;
20 import static org.mockito.internal.util.MockUtil.resetMock;
21 import static org.mockito.internal.util.MockUtil.typeMockabilityOf;
22 import static org.mockito.internal.verification.VerificationModeFactory.noMoreInteractions;
23 
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.Set;
27 import org.mockito.InOrder;
28 import org.mockito.MockSettings;
29 import org.mockito.MockingDetails;
30 import org.mockito.exceptions.base.MockitoException;
31 import org.mockito.exceptions.misusing.NotAMockException;
32 import org.mockito.internal.creation.MockSettingsImpl;
33 import org.mockito.internal.invocation.finder.VerifiableInvocationsFinder;
34 import org.mockito.internal.progress.MockingProgress;
35 import org.mockito.internal.stubbing.InvocationContainer;
36 import org.mockito.internal.stubbing.OngoingStubbingImpl;
37 import org.mockito.internal.stubbing.StubberImpl;
38 import org.mockito.internal.util.DefaultMockingDetails;
39 import org.mockito.internal.verification.MockAwareVerificationMode;
40 import org.mockito.internal.verification.VerificationDataImpl;
41 import org.mockito.internal.verification.VerificationModeFactory;
42 import org.mockito.internal.verification.api.InOrderContext;
43 import org.mockito.internal.verification.api.VerificationDataInOrder;
44 import org.mockito.internal.verification.api.VerificationDataInOrderImpl;
45 import org.mockito.invocation.Invocation;
46 import org.mockito.listeners.VerificationListener;
47 import org.mockito.mock.MockCreationSettings;
48 import org.mockito.stubbing.OngoingStubbing;
49 import org.mockito.stubbing.Stubber;
50 import org.mockito.verification.VerificationMode;
51 
52 @SuppressWarnings("unchecked")
53 public class MockitoCore {
54 
isTypeMockable(Class<?> typeToMock)55     public boolean isTypeMockable(Class<?> typeToMock) {
56         return typeMockabilityOf(typeToMock).mockable();
57     }
58 
mock(Class<T> typeToMock, MockSettings settings)59     public <T> T mock(Class<T> typeToMock, MockSettings settings) {
60         if (!MockSettingsImpl.class.isInstance(settings)) {
61             throw new IllegalArgumentException("Unexpected implementation of '" + settings.getClass().getCanonicalName() + "'\n" + "At the moment, you cannot provide your own implementations of that class.");
62         }
63         MockSettingsImpl impl = MockSettingsImpl.class.cast(settings);
64         MockCreationSettings<T> creationSettings = impl.confirm(typeToMock);
65         T mock = createMock(creationSettings);
66         mockingProgress().mockingStarted(mock, creationSettings);
67         return mock;
68     }
69 
when(T methodCall)70     public <T> OngoingStubbing<T> when(T methodCall) {
71         MockingProgress mockingProgress = mockingProgress();
72         mockingProgress.stubbingStarted();
73         @SuppressWarnings("unchecked")
74         OngoingStubbing<T> stubbing = (OngoingStubbing<T>) mockingProgress.pullOngoingStubbing();
75         if (stubbing == null) {
76             mockingProgress.reset();
77             throw missingMethodInvocation();
78         }
79         return stubbing;
80     }
81 
verify(T mock, VerificationMode mode)82     public <T> T verify(T mock, VerificationMode mode) {
83         if (mock == null) {
84             throw nullPassedToVerify();
85         }
86         if (!isMock(mock)) {
87             throw notAMockPassedToVerify(mock.getClass());
88         }
89         MockingProgress mockingProgress = mockingProgress();
90         VerificationMode actualMode = mockingProgress.maybeVerifyLazily(mode);
91         mockingProgress.verificationStarted(new MockAwareVerificationMode(mock, actualMode, mockingProgress.verificationListeners()));
92         return mock;
93     }
94 
reset(T... mocks)95     public <T> void reset(T... mocks) {
96         MockingProgress mockingProgress = mockingProgress();
97         mockingProgress.validateState();
98         mockingProgress.reset();
99         mockingProgress.resetOngoingStubbing();
100 
101         for (T m : mocks) {
102             resetMock(m);
103         }
104     }
105 
clearInvocations(T... mocks)106     public <T> void clearInvocations(T... mocks) {
107         MockingProgress mockingProgress = mockingProgress();
108         mockingProgress.validateState();
109         mockingProgress.reset();
110         mockingProgress.resetOngoingStubbing();
111 
112         for (T m : mocks) {
113             getMockHandler(m).getInvocationContainer().clearInvocations();
114         }
115     }
116 
verifyNoMoreInteractions(Object... mocks)117     public void verifyNoMoreInteractions(Object... mocks) {
118         assertMocksNotEmpty(mocks);
119         mockingProgress().validateState();
120         for (Object mock : mocks) {
121             try {
122                 if (mock == null) {
123                     throw nullPassedToVerifyNoMoreInteractions();
124                 }
125                 InvocationContainer invocations = getMockHandler(mock).getInvocationContainer();
126                 VerificationDataImpl data = new VerificationDataImpl(invocations, null);
127                 noMoreInteractions().verify(data);
128             } catch (NotAMockException e) {
129                 throw notAMockPassedToVerifyNoMoreInteractions();
130             }
131         }
132     }
133 
verifyNoMoreInteractionsInOrder(List<Object> mocks, InOrderContext inOrderContext)134     public void verifyNoMoreInteractionsInOrder(List<Object> mocks, InOrderContext inOrderContext) {
135         mockingProgress().validateState();
136         VerificationDataInOrder data = new VerificationDataInOrderImpl(inOrderContext, VerifiableInvocationsFinder.find(mocks), null);
137         VerificationModeFactory.noMoreInteractions().verifyInOrder(data);
138     }
139 
assertMocksNotEmpty(Object[] mocks)140     private void assertMocksNotEmpty(Object[] mocks) {
141         if (mocks == null || mocks.length == 0) {
142             throw mocksHaveToBePassedToVerifyNoMoreInteractions();
143         }
144     }
145 
inOrder(Object... mocks)146     public InOrder inOrder(Object... mocks) {
147         if (mocks == null || mocks.length == 0) {
148             throw mocksHaveToBePassedWhenCreatingInOrder();
149         }
150         for (Object mock : mocks) {
151             if (mock == null) {
152                 throw nullPassedWhenCreatingInOrder();
153             }
154             if (!isMock(mock)) {
155                 throw notAMockPassedWhenCreatingInOrder();
156             }
157         }
158         return new InOrderImpl(Arrays.asList(mocks));
159     }
160 
stubber()161     public Stubber stubber() {
162         MockingProgress mockingProgress = mockingProgress();
163         mockingProgress.stubbingStarted();
164         mockingProgress.resetOngoingStubbing();
165         return new StubberImpl();
166     }
167 
validateMockitoUsage()168     public void validateMockitoUsage() {
169         mockingProgress().validateState();
170     }
171 
172     /**
173      * For testing purposes only. Is not the part of main API.
174      *
175      * @return last invocation
176      */
getLastInvocation()177     public Invocation getLastInvocation() {
178         OngoingStubbingImpl ongoingStubbing = ((OngoingStubbingImpl) mockingProgress().pullOngoingStubbing());
179         List<Invocation> allInvocations = ongoingStubbing.getRegisteredInvocations();
180         return allInvocations.get(allInvocations.size() - 1);
181     }
182 
ignoreStubs(Object... mocks)183     public Object[] ignoreStubs(Object... mocks) {
184         for (Object m : mocks) {
185             InvocationContainer invocationContainer = getMockHandler(m).getInvocationContainer();
186             List<Invocation> ins = invocationContainer.getInvocations();
187             for (Invocation in : ins) {
188                 if (in.stubInfo() != null) {
189                     in.ignoreForVerification();
190                 }
191             }
192         }
193         return mocks;
194     }
195 
mockingDetails(Object toInspect)196     public MockingDetails mockingDetails(Object toInspect) {
197         return new DefaultMockingDetails(toInspect);
198     }
199 }
200