@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"); }
@Override public void reportInvocation(MethodInvocationReport methodInvocationReport) { if(! active){ return; } DescribedInvocation di = methodInvocationReport.getInvocation(); MethodDescriptor md = null; if(di instanceof InvocationOnMock){ InvocationOnMock impl = (InvocationOnMock) di; Method method = impl.getMethod(); md = new MethodDescriptor(method, retvalType); } else { //hopefully it should never happen md = getMethodDescriptor_old(di); } if(md.getMethodName().equals("finalize")){ //ignore it, otherwise if we mock it, we ll end up in a lot of side effects... :( return; } if(onlyMockAbstractMethods() && !md.getGenericMethod().isAbstract()) { return; } synchronized (map){ MethodDescriptor current = map.get(md.getID()); if(current == null){ current = md; } current.increaseCounter(); map.put(md.getID(),current); } }
public void reportInvocation(MethodInvocationReport methodInvocationReport) { printHeader(); printStubInfo(methodInvocationReport); printInvocation(methodInvocationReport.getInvocation()); printReturnedValueOrThrowable(methodInvocationReport); printFooter(); }
private void printReturnedValueOrThrowable(MethodInvocationReport methodInvocationReport) { if (methodInvocationReport.threwException()) { String message = methodInvocationReport.getThrowable().getMessage() == null ? "" : " with message " + methodInvocationReport.getThrowable().getMessage(); printlnIndented("has thrown: " + methodInvocationReport.getThrowable().getClass() + message); } else { String type = (methodInvocationReport.getReturnedValue() == null) ? "" : " (" + methodInvocationReport.getReturnedValue().getClass().getName() + ")"; printlnIndented("has returned: \"" + methodInvocationReport.getReturnedValue() + "\"" + type); } }
@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 public void should_report_listener_exception() throws Throwable { willThrow(new NullPointerException()).given(customListener).reportInvocation(any(MethodInvocationReport.class)); try { notifier.handle(invocation); fail(); } catch (MockitoException me) { assertThat(me.getMessage()) .contains("invocation listener") .contains("CustomListener") .contains("threw an exception") .contains("NullPointerException"); } }
@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); }
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(); } } } }); }
@Override public void reportInvocation(MethodInvocationReport methodInvocationReport) { }
@Override public void reportInvocation(MethodInvocationReport methodInvocationReport) { System.out.println("" + "Calling method " + methodInvocationReport.getInvocation().toString() + " and result is " + methodInvocationReport.getReturnedValue()); }
private void printStubInfo(MethodInvocationReport methodInvocationReport) { if (methodInvocationReport.getLocationOfStubbing() != null) { printlnIndented("stubbed: " + methodInvocationReport.getLocationOfStubbing()); } }
public void reportInvocation(MethodInvocationReport mcr) { this.invocation = mcr.getInvocation(); this.returnValue = mcr.getReturnedValue(); this.locationOfStubbing = mcr.getLocationOfStubbing(); }
public void reportInvocation(MethodInvocationReport methodInvocationReport) { // nop }