public static void notifyStubbedAnswerLookup(Invocation invocation, Stubbing stubbingFound, Collection<Stubbing> allStubbings, CreationSettings creationSettings) { List<StubbingLookupListener> listeners = creationSettings.getStubbingLookupListeners(); if (listeners.isEmpty()) { return; } StubbingLookupEvent event = new Event(invocation, stubbingFound, allStubbings, creationSettings); for (StubbingLookupListener listener : listeners) { listener.onStubbingLookup(event); } }
public void mockingStarted(Object mock, Class classToMock) { if (listener != null && listener instanceof MockingStartedListener) { ((MockingStartedListener) listener).mockingStarted(mock, classToMock); } validateMostStuff(); }
public void onStubbingLookup(StubbingLookupEvent event) { Strictness actualStrictness = determineStrictness(event.getStubbingFound(), event.getMockSettings(), currentStrictness); if (actualStrictness != Strictness.STRICT_STUBS) { return; } if (event.getStubbingFound() == null) { //If stubbing was not found for invocation it means that either the mock invocation was not stubbed or //we have a stubbing arg mismatch. List<Invocation> argMismatchStubbings = potentialArgMismatches(event.getInvocation(), event.getAllStubbings()); if (!argMismatchStubbings.isEmpty()) { mismatchesReported = true; Reporter.potentialStubbingProblem(event.getInvocation(), argMismatchStubbings); } } else { //when strict stubs are in use, every time a stub is realized in the code it is implicitly marked as verified //this way, the users don't have to repeat themselves to verify stubbed invocations (DRY) event.getInvocation().markVerified(); } }
public WarningsCollector() { createdMocks = new LinkedList(); MockingProgress progress = new ThreadSafeMockingProgress(); progress.setListener(new CollectCreatedMocks(createdMocks)); }
private void notifyMethodCallException(Invocation invocation, Throwable exception) { for (InvocationListener listener : invocationListeners) { try { listener.reportInvocation(new NotifiedMethodInvocationReport(invocation, exception)); } catch(Throwable listenerThrowable) { new Reporter().invocationListenerThrewException(listener, listenerThrowable); } } }
static void addListener(MockitoListener listener, Set<MockitoListener> listeners) { List<MockitoListener> delete = new LinkedList<MockitoListener>(); for (MockitoListener existing : listeners) { if (existing.getClass().equals(listener.getClass())) { if (existing instanceof AutoCleanableListener && ((AutoCleanableListener) existing).isListenerDirty()) { //dirty listener means that there was an exception even before the test started //if we fail here with redundant mockito listener exception there will be multiple failures causing confusion //so we simply remove the existing listener and move on delete.add(existing); } else { Reporter.redundantMockitoListener(listener.getClass().getSimpleName()); } } } //delete dirty listeners so they don't occupy state/memory and don't receive notifications for (MockitoListener toDelete : delete) { listeners.remove(toDelete); } listeners.add(listener); }
public static Object notifyVerificationStarted(List<VerificationStartedListener> listeners, MockingDetails originalMockingDetails) { if (listeners.isEmpty()) { return originalMockingDetails.getMock(); } VerificationStartedEvent event = new Event(originalMockingDetails); for (VerificationStartedListener listener : listeners) { listener.onVerificationStarted(event); } return event.getMock(); }
public void setMock(Object mock) { if (mock == null) { throw Reporter.methodDoesNotAcceptParameter("VerificationStartedEvent.setMock", "null parameter."); } MockingDetails mockingDetails = Mockito.mockingDetails(mock); if (!mockingDetails.isMock()) { throw Reporter.methodDoesNotAcceptParameter("VerificationStartedEvent.setMock", "parameter which is not a Mockito mock.\n" + " Received parameter: " + ValuePrinter.print(mock) + ".\n "); } MockCreationSettings originalMockSettings = this.originalMockingDetails.getMockCreationSettings(); assertCompatibleTypes(mock, originalMockSettings); this.mock = mock; }
public <T> T verify(T mock, VerificationMode mode) { if (mock == null) { throw nullPassedToVerify(); } MockingDetails mockingDetails = mockingDetails(mock); if (!mockingDetails.isMock()) { throw notAMockPassedToVerify(mock.getClass()); } assertNotStubOnlyMock(mock); MockHandler handler = mockingDetails.getMockHandler(); mock = (T) VerificationStartedNotifier.notifyVerificationStarted( handler.getMockSettings().getVerificationStartedListeners(), mockingDetails); MockingProgress mockingProgress = mockingProgress(); VerificationMode actualMode = mockingProgress.maybeVerifyLazily(mode); mockingProgress.verificationStarted(new MockAwareVerificationMode(mock, actualMode, mockingProgress.verificationListeners())); return mock; }
notifyStubbedAnswerLookup(invocation, stubbing, invocationContainer.getStubbingsAscending(), (CreationSettings) mockSettings);
public class MocksCollector { private final List<Object> createdMocks; public MocksCollector() { createdMocks = new LinkedList<Object>(); final MockingProgress progress = new ThreadSafeMockingProgress(); progress.setListener(new CollectCreatedMocks(createdMocks)); } public Object[] getMocks() { return createdMocks.toArray(); } }
private void notifyMethodCall(Invocation invocation, Object returnValue) { for (InvocationListener listener : invocationListeners) { try { listener.reportInvocation(new NotifiedMethodInvocationReport(invocation, returnValue)); } catch(Throwable listenerThrowable) { new Reporter().invocationListenerThrewException(listener, listenerThrowable); } } }
public void mockingStarted(Object mock, Class classToMock, MockSettings mockSettings) { if (listener != null && listener instanceof MockingStartedListener) { ((MockingStartedListener) listener).mockingStarted(mock, classToMock, mockSettings); } validateMostStuff(); }
public WarningsCollector() { createdMocks = new LinkedList(); MockingProgress progress = new ThreadSafeMockingProgress(); progress.setListener(new CollectCreatedMocks(createdMocks)); }
private void notifyMethodCall(Invocation invocation, Object returnValue) { for (InvocationListener listener : invocationListeners) { try { listener.reportInvocation(new NotifiedMethodInvocationReport(invocation, returnValue)); } catch(Throwable listenerThrowable) { new Reporter().invocationListenerThrewException(listener, listenerThrowable); } } }
private void notifyMethodCallException(Invocation invocation, Throwable exception) { for (InvocationListener listener : invocationListeners) { try { listener.reportInvocation(new NotifiedMethodInvocationReport(invocation, exception)); } catch(Throwable listenerThrowable) { new Reporter().invocationListenerThrewException(listener, listenerThrowable); } } }