public void testMyFunction() throws Exception { Application mock = mock(Application.class); when(mock.myFunction(anyString())).thenAnswer(new Answer<String>() { @Override public String answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); return (String) args[0]; } }); assertEquals("someString",mock.myFunction("someString")); assertEquals("anotherString",mock.myFunction("anotherString")); }
World mockWorld = mock(World.class); doAnswer(new Answer<Void>() { public Void answer(InvocationOnMock invocation) { Object[] args = invocation.getArguments(); System.out.println("called with arguments: " + Arrays.toString(args)); return null; } }).when(mockWorld).setState(anyString());
@Override public ConnectivityMonitor answer(InvocationOnMock invocation) { connectivityListener = (ConnectivityListener) invocation.getArguments()[1]; return connectivityMonitor; } });
@Override public byte[] answer(InvocationOnMock invocation) throws Throwable { int size = (Integer) invocation.getArguments()[0]; return new byte[size]; } });
private boolean wantedArgumentPositionIsValidForInvocation(InvocationOnMock invocation, int argumentPosition) { if (argumentPosition < 0) { return false; } if (!invocation.getMethod().isVarArgs()) { return invocation.getArguments().length > argumentPosition; } // for all varargs accepts positive ranges return true; }
@Override public Resource<Bitmap> answer(InvocationOnMock invocationOnMock) throws Throwable { @SuppressWarnings("unchecked") Resource<Bitmap> input = (Resource<Bitmap>) invocationOnMock.getArguments()[1]; return input; } }
@Override public Object answer(InvocationOnMock invocation) throws Throwable { char[] chars = (char[]) invocation.getArguments()[0]; char[] copy = new char[chars.length]; System.arraycopy(chars, 0, copy, 0, chars.length); invocation.getArguments()[0] = copy; return null; } }
@Override public Object answer(InvocationOnMock invocation) throws Throwable { invocation.getArguments()[3] = new AttributesImpl((Attributes) invocation.getArguments()[3]); return null; } }).given(contentHandler).startElement(anyString(), anyString(), anyString(), any(Attributes.class));
@Override public Integer answer(InvocationOnMock invocation) throws Throwable { return byteStream.read((byte[]) invocation.getArguments()[0]); } });
@Override public Bitmap answer(InvocationOnMock invocation) throws Throwable { int width = (Integer) invocation.getArguments()[0]; int height = (Integer) invocation.getArguments()[1]; Bitmap.Config config = (Bitmap.Config) invocation.getArguments()[2]; return Bitmap.createBitmap(width, height, config); } }
@Override public Integer answer(InvocationOnMock invocation) throws Throwable { return byteArrayInputStream.read((byte[]) invocation.getArguments()[0]); } });
@Override public Boolean answer(InvocationOnMock invocation) { Runnable runnable = (Runnable) invocation.getArguments()[0]; runnable.run(); return true; } });
@Override public Integer answer(InvocationOnMock invocation) throws Throwable { byte[] bytes = (byte[]) invocation.getArguments()[0]; ByteArrayInputStream inputStream = new ByteArrayInputStream(TEXT.getBytes()); return inputStream.read(bytes); } });
@Override public Object answer(InvocationOnMock invocationOnMock) throws Throwable { ResourceCallback cb = (ResourceCallback) invocationOnMock.getArguments()[invocationOnMock.getArguments().length - 2]; cb.onResourceReady(resource, DataSource.REMOTE); return null; } }
@Override public Void answer(InvocationOnMock invocationOnMock) throws Throwable { MessageDigest md = (MessageDigest) invocationOnMock.getArguments()[0]; md.update(toWrite.getBytes("UTF-8")); return null; } }
@Override public Object answer(InvocationOnMock invocationOnMock) throws Throwable { SizeReadyCallback cb = (SizeReadyCallback) invocationOnMock.getArguments()[0]; cb.onSizeReady(width, height); return null; } }
@Override public Resource<?> answer(InvocationOnMock invocationOnMock) throws Throwable { BitmapResource resource = (BitmapResource) invocationOnMock.getArguments()[1]; bitmaps.add(resource.get()); return null; } }
@Override public Void answer(InvocationOnMock invocationOnMock) { Resource<?> resource = (Resource<?>) invocationOnMock.getArguments()[1]; assertEquals(expected, resource.get()); return null; } })
protected TextMessage createTextMessage(MessageCreator creator) throws JMSException { Session mock = mock(Session.class); given(mock.createTextMessage(BDDMockito.any())).willAnswer( (Answer<TextMessage>) invocation -> new StubTextMessage((String) invocation.getArguments()[0])); javax.jms.Message message = creator.createMessage(mock); verify(mock).createTextMessage(BDDMockito.any()); return TextMessage.class.cast(message); }
private void invokeMessageCreator() { willAnswer(invocation -> { MessageCreator messageCreator = (MessageCreator) invocation.getArguments()[1]; messageCreator.createMessage(null); return null; }).given(this.jmsTemplate).send(eq("myQueue"), any()); }