I always though this produced nicely readable tests for async calls:

http://blog.reflectedcircle.co.uk/2009/12/08/gwt-rpc-asynccallback-testing-using-mockito/

Greetz


On Aug 12, 11:24 am, Magno Machado <magn...@gmail.com> wrote:
> Code from a @Test method:
> List<PesquisaProxy> pesquisas = new ArrayList<PesquisaProxy>();
>  Request<?> request = mock(Request.class);
> doReturn(request).when(pesquisaRequest).listAll();
> doReturn(pesquisaRequest).when(requestFactory).pesquisaRequest();
> doAnswer(RequestFactoryUtils.ok(pesquisas)).when(request).fire(RequestFacto 
> ryUtils.anyReceiver());
>
> And here my RequestFactoryUtils.ok:
> public static <T> Answer<T> ok(final T result) {
> return new Answer<T>() {
>
> @Override
> public T answer(InvocationOnMock invocation) throws Throwable {
> Object[] args = invocation.getArguments();
> Object _receiver = args[args.length - 1];
> Receiver<T> receiver = (Receiver<T>)_receiver;
> receiver.onSuccess(result);
> return null;
>
>
>
>
>
>
>
>
>
> }
>  };
> }
> On Thu, Aug 11, 2011 at 6:32 PM, objectuser <kevin.k.le...@gmail.com> wrote:
> > Here's how I do it.
> >     @Test
> >     public void testAsync() {
> >         doAnswer(new Answer<Void>() {
>
> >             @Override
> >             public Void answer(InvocationOnMock invocation) throws
> > Throwable {
> >                 AsyncCallback<CommandResult> callback =
> > (AsyncCallback<CommandResult>) invocation.getArguments()[1];
> >                 callback.onSuccess(new CommandResult());
> >                 return null;
> >             }
> >         }).when(commandProcessor).execute(any(Command.class),
> > any(AsyncCallback.class));
>
> >         // invoke something that sends the command ... then verify the
> > results
> >         verify(...)...;
> >     }
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Google Web Toolkit" group.
> > To view this discussion on the web visit
> >https://groups.google.com/d/msg/google-web-toolkit/-/xx0OrJ46MUwJ.
>
> > To post to this group, send email to google-web-toolkit@googlegroups.com.
> > To unsubscribe from this group, send email to
> > google-web-toolkit+unsubscr...@googlegroups.com.
> > For more options, visit this group at
> >http://groups.google.com/group/google-web-toolkit?hl=en.
>
> --
> Magno Machado 
> Paulohttp://blog.magnomachado.com.brhttp://code.google.com/p/emballo/

-- 
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to 
google-web-toolkit+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/google-web-toolkit?hl=en.

Reply via email to