Java 类org.mockito.listeners.InvocationListener 实例源码

项目:astor    文件:InvocationListenerCallbackTest.java   
@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));
    }
}
项目:monarch    文件:OffHeapStoredObjectAddressStackJUnitTest.java   
@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");
}
项目:springmock    文件:InvocationListenersSettings.java   
private InvocationListener[] createListeners() {
    final List<InvocationListener> listeners = listenerClasses
            .stream()
            .map(BeanUtils::instantiateClass)
            .collect(toList());
    return listeners.toArray(new InvocationListener[listeners.size()]);
}
项目:springmock    文件:HasInvocationListener.java   
@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));
    }
}
项目:springmock    文件:HasInvocationListener.java   
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));
}
项目:j2objc    文件:MockSettingsImpl.java   
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;
}
项目:j2objc    文件:MockSettingsImpl.java   
private boolean invocationListenersContainsType(Class<?> clazz) {
    for (InvocationListener listener : invocationListeners) {
        if (listener.getClass().equals(clazz)) {
            return true;
        }
    }
    return false;
}
项目:astor    文件:InvocationNotifierHandler.java   
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);
           }
       }
}
项目:astor    文件:InvocationNotifierHandler.java   
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);
           }
       }
}
项目:astor    文件:MockSettingsImpl.java   
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;
}
项目:astor    文件:MockSettingsImpl.java   
private boolean invocationListenersContainsType(Class<?> clazz) {
    for (InvocationListener listener : invocationListeners) {
        if (listener.getClass().equals(clazz)) {
            return true;
        }
    }
    return false;
}
项目:astor    文件:ListenersLostOnResetMockTest.java   
@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));
}
项目:astor    文件:MockHandlerImplTest.java   
@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);
}
项目:astor    文件:MockHandlerImplTest.java   
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;
}
项目:mockito-async    文件:Await.java   
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();
                }
            }
        }
    });
}
项目:springmock    文件:InvocationListenersSettings.java   
public InvocationListenersSettings(Class<? extends InvocationListener>[] listenerClasses) {
    this.listenerClasses = asList(listenerClasses);
}
项目:springmock    文件:MockitoDoubleConfiguration.java   
public MockitoDoubleConfigurationBuilder invocationListeners(Class<? extends InvocationListener>[] listeners) {
    return addSettings(new InvocationListenersSettings(listeners));
}
项目:springmock    文件:HasInvocationListener.java   
public static Matcher<? extends InvocationListener> hasInstancesOf(Class<? extends InvocationListener> ... expectedClasses) {
    return new HasInvocationListener(asList(expectedClasses));
}
项目:springmock    文件:HasInvocationListener.java   
private HasInvocationListener(Collection<Class<? extends InvocationListener>> expectedListenerClass) {
    this.expectedListenerClass = expectedListenerClass;
}
项目:j2objc    文件:MockSettingsImpl.java   
public List<InvocationListener> getInvocationListeners() {
    return this.invocationListeners;
}
项目:astor    文件:Reporter.java   
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);
}
项目:astor    文件:CreationSettings.java   
public List<InvocationListener> getInvocationListeners() {
    return invocationListeners;
}
项目:astor    文件:MockSettingsImpl.java   
public List<InvocationListener> getInvocationListeners() {
    return this.invocationListeners;
}
项目:astor    文件:MockHandlerImplTest.java   
private MockHandlerImpl<?> createCorrectlyStubbedHandler(InvocationListener throwingListener) {
    MockHandlerImpl<?> handler = createHandlerWithListeners(throwingListener);
    stubOrdinaryInvocationWithGivenReturnValue(handler);
    return handler;
}
项目:astor    文件:MockCreationSettings.java   
/**
 * the invocation listeners attached to this mock, see {@link org.mockito.MockSettings#invocationListeners}.
 */
List<InvocationListener> getInvocationListeners();
项目:astor    文件:MockSettings.java   
/**
 * 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);