@Test public void should_call_all_listener_when_mock_throws_exception() throws Exception { // given InvocationListener listener1 = mock(InvocationListener.class, "listener1"); InvocationListener listener2 = mock(InvocationListener.class, "listener2"); Foo foo = mock(Foo.class, withSettings().invocationListeners(listener1, listener2)); doThrow(new OvenNotWorking()).when(foo).doSomething("cook"); // when try { foo.doSomething("cook"); fail("Exception expected."); } catch (OvenNotWorking actualException) { // then InOrder orderedVerify = inOrder(listener1, listener2); orderedVerify.verify(listener1).reportInvocation(any(MethodInvocationReport.class)); orderedVerify.verify(listener2).reportInvocation(any(MethodInvocationReport.class)); } }
@Test public void defaultStackLogsNothing() { OffHeapStoredObjectAddressStack stack = new OffHeapStoredObjectAddressStack(); Logger lw = mock(Logger.class, withSettings().invocationListeners(new InvocationListener() { @Override public void reportInvocation(MethodInvocationReport methodInvocationReport) { fail("Unexpected invocation"); } })); stack.logSizes(lw, "should not be used"); }
private InvocationListener[] createListeners() { final List<InvocationListener> listeners = listenerClasses .stream() .map(BeanUtils::instantiateClass) .collect(toList()); return listeners.toArray(new InvocationListener[listeners.size()]); }
@Override public boolean matches(Object argument) { if(argument.getClass().isArray()) { final InvocationListener [] listeners = (InvocationListener[]) argument; return matches(asList(listeners)); } else { final InvocationListener listener = (InvocationListener) argument; return matches(singleton(listener)); } }
private boolean matches(Collection<InvocationListener> listeners) { final Collection<Class> actualListenerClasses = listeners.stream() .map(Object::getClass) .collect(Collectors.toList()); return Objects.equals(actualListenerClasses.size(), expectedListenerClass.size()) && Objects.equals(new HashSet<>(actualListenerClasses), new HashSet<>(expectedListenerClass)); }
public MockSettings invocationListeners(InvocationListener... listeners) { if (listeners == null || listeners.length == 0) { new Reporter().invocationListenersRequiresAtLeastOneListener(); } for (InvocationListener listener : listeners) { if (listener == null) { new Reporter().invocationListenerDoesNotAcceptNullParameters(); } this.invocationListeners.add(listener); } return this; }
private boolean invocationListenersContainsType(Class<?> clazz) { for (InvocationListener listener : invocationListeners) { if (listener.getClass().equals(clazz)) { return true; } } return false; }
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); } } }
@Test public void listener() throws Exception { InvocationListener invocationListener = mock(InvocationListener.class); List mockedList = mock(List.class, withSettings().invocationListeners(invocationListener)); reset(mockedList); mockedList.clear(); verify(invocationListener).reportInvocation(any(MethodInvocationReport.class)); }
@Test(expected = MockitoException.class) public void shouldThrowMockitoExceptionWhenInvocationHandlerThrowsAnything() throws Throwable { // given InvocationListener throwingListener = mock(InvocationListener.class); doThrow(new Throwable()).when(throwingListener).reportInvocation(any(MethodInvocationReport.class)); MockHandlerImpl<?> handler = createCorrectlyStubbedHandler(throwingListener); // when handler.handle(invocation); }
private MockHandlerImpl<?> createHandlerWithListeners(InvocationListener... listener) { @SuppressWarnings("rawtypes") MockHandlerImpl<?> handler = new MockHandlerImpl(mock(MockSettingsImpl.class)); handler.matchersBinder = mock(MatchersBinder.class); given(handler.getMockSettings().getInvocationListeners()).willReturn(Arrays.asList(listener)); return handler; }
public static MockSettings async(MockSettings settings) { return settings.invocationListeners(new InvocationListener() { @Override public void reportInvocation(MethodInvocationReport methodInvocationReport) { DescribedInvocation invocation = methodInvocationReport.getInvocation(); if (invocation instanceof InvocationOnMock) { Object mock = ((InvocationOnMock) invocation).getMock(); synchronized (mock) { mock.notifyAll(); } } } }); }
public InvocationListenersSettings(Class<? extends InvocationListener>[] listenerClasses) { this.listenerClasses = asList(listenerClasses); }
public MockitoDoubleConfigurationBuilder invocationListeners(Class<? extends InvocationListener>[] listeners) { return addSettings(new InvocationListenersSettings(listeners)); }
public static Matcher<? extends InvocationListener> hasInstancesOf(Class<? extends InvocationListener> ... expectedClasses) { return new HasInvocationListener(asList(expectedClasses)); }
private HasInvocationListener(Collection<Class<? extends InvocationListener>> expectedListenerClass) { this.expectedListenerClass = expectedListenerClass; }
public List<InvocationListener> getInvocationListeners() { return this.invocationListeners; }
public void invocationListenerThrewException(InvocationListener listener, Throwable listenerThrowable) { throw new MockitoException(StringJoiner.join( "The invocation listener with type " + listener.getClass().getName(), "threw an exception : " + listenerThrowable.getClass().getName() + listenerThrowable.getMessage()), listenerThrowable); }
public List<InvocationListener> getInvocationListeners() { return invocationListeners; }
private MockHandlerImpl<?> createCorrectlyStubbedHandler(InvocationListener throwingListener) { MockHandlerImpl<?> handler = createHandlerWithListeners(throwingListener); stubOrdinaryInvocationWithGivenReturnValue(handler); return handler; }
/** * the invocation listeners attached to this mock, see {@link org.mockito.MockSettings#invocationListeners}. */ List<InvocationListener> getInvocationListeners();
/** * Registers a listener for method invocations on this mock. The listener is * notified every time a method on this mock is called. * <p> * Multiple listeners may be added, but the same object is only added once. * The order, in which the listeners are added, is not guaranteed to be the * order in which the listeners are notified. * * Example: * <pre class="code"><code class="java"> * List mockWithListener = mock(List.class, withSettings().invocationListeners(new YourInvocationListener())); * </code></pre> * * See the {@link InvocationListener listener interface} for more details. * * @param listeners The invocation listeners to add. May not be null. * @return settings instance so that you can fluently specify other settings */ MockSettings invocationListeners(InvocationListener... listeners);