/** * Throw an exception from the mock/spy only in the case that the * call stack at the time the method has a line which matches the given * pattern. * * @param t the Throwable to throw * @param pattern the pattern against which to match the call stack trace * @return the stub in progress */ public static Stubber doThrowWhenCallStackMatches( final Throwable t, final String pattern) { return Mockito.doAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { t.setStackTrace(Thread.currentThread().getStackTrace()); for (StackTraceElement elem : t.getStackTrace()) { if (elem.toString().matches(pattern)) { throw t; } } return invocation.callRealMethod(); } }); }
public static Stubber answerStringByReference(String value) { return doAnswer((Answer<Integer>) invocation -> { Object[] args = invocation.getArguments(); PointerByReference reference = (PointerByReference)args[0]; Pointer pointer = new Memory(Native.WCHAR_SIZE * (value.length() +1)); pointer.setWideString(0, value); reference.setValue(pointer); return 0; }); }
public static Stubber answerIntByReference(int value) { return doAnswer((Answer<Integer>) invocation -> { Object[] args = invocation.getArguments(); IntByReference reference = (IntByReference)args[0]; reference.setValue(value); return 0; }); }
private Stubber doUpdateInto(final List<Update> updates) { return doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { //noinspection unchecked Iterables.addAll(updates, (Iterable<Update>) invocation.getArguments()[0]); return null; } }); }
private Stubber captureUpdatesAndTags(final List<Update> updates, final Set<String> tags) { return doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { //noinspection unchecked Iterables.addAll(updates, (Iterable<Update>) invocation.getArguments()[0]); //noinspection unchecked tags.addAll((Collection<? extends String>) invocation.getArguments()[1]); return null; } }); }
public static Stubber doThrow(final Throwable toBeThrown) { return AccessController.doPrivileged(new PrivilegedAction<Stubber>() { @Override public Stubber run() { return org.mockito.Mockito.doThrow(toBeThrown); } }); }
public static Stubber doThrow(final Class<? extends Throwable> toBeThrown) { return AccessController.doPrivileged(new PrivilegedAction<Stubber>() { @Override public Stubber run() { return org.mockito.Mockito.doThrow(toBeThrown); } }); }
public static Stubber doCallRealMethod() { return AccessController.doPrivileged(new PrivilegedAction<Stubber>() { @Override public Stubber run() { return org.mockito.Mockito.doCallRealMethod(); } }); }
public static Stubber doAnswer(final Answer answer) { return AccessController.doPrivileged(new PrivilegedAction<Stubber>() { @Override public Stubber run() { return org.mockito.Mockito.doAnswer(answer); } }); }
public static Stubber doNothing() { return AccessController.doPrivileged(new PrivilegedAction<Stubber>() { @Override public Stubber run() { return org.mockito.Mockito.doNothing(); } }); }
public static Stubber doReturn(final Object toBeReturned) { return AccessController.doPrivileged(new PrivilegedAction<Stubber>() { @Override public Stubber run() { return org.mockito.Mockito.doReturn(toBeReturned); } }); }
public static Stubber copyToParameter(byte[] result) { return Mockito.doAnswer((x) -> { byte[] dst = (byte[])x.getArguments()[0]; System.arraycopy(result, 0, dst, 0, result.length); return null; }); }
/** * Extend Mockito API by allowing an arbitrary number of inputs to * the method {@code doReturn()} * * @param value * @param values * @return */ public static Stubber doReturn(Object value, Object... values){ Stubber stubber = Mockito.doReturn(value); for(Object v : values){ stubber = stubber.doReturn(v); } return stubber; }
@SuppressWarnings("unchecked") public <T> Stubber toAnswer(final Runnable method) { return Mockito.doAnswer(invocation -> { Object[] arguments = invocation.getArguments(); method.run(); return null; }); }
private Stubber doAnswer(final Consumer<Object[]> method, final int argumentCount) { return Mockito.doAnswer(invocation -> { Object[] arguments = invocation.getArguments(); if (arguments.length >= argumentCount) { method.accept(arguments); } else { failure("Invocation requires at least " + argumentCount + " argument"); } return null; }); }
public Stubber spyReturnValue() { return Mockito.doAnswer(new Answer<T>() { public T answer(InvocationOnMock invocation) throws Throwable { return setupSpy(invocation.callRealMethod()); } }); }
public Stubber spyParameter(final int parameterIdx) { return Mockito.doAnswer(new Answer() { public Object answer(InvocationOnMock invocation) throws Throwable { Object[] arguments = invocation.getArguments(); arguments[parameterIdx] = setupSpy(arguments[parameterIdx]); return invocation.callRealMethod(); } }); }
private Stubber injectIOE() { return futureThrows(new IOException("Injected")); }
static <V> Stubber futureReturns(V value) { ListenableFuture<V> ret = Futures.immediateFuture(value); return Mockito.doReturn(ret); }
static Stubber futureThrows(Throwable t) { ListenableFuture<?> ret = Futures.immediateFailedFuture(t); return Mockito.doReturn(ret); }