Re: How to use Mockito for testing Async calls?

2011-08-14 Thread pete
And of course you can just define your Async interface methods by

Request myAsyncMethod(AsyncCallback callback);

instead of

void myAsyncMethod(AsyncCallback callback);

then you can use normal Mockito-syntax, ala

when(service.myAsyncMethod(any(AsyncCallback.class))).thenAnswer(new
Answer(){
Request answer(InvocationOnMock invocation) {
 Object[] args = invocation.getArguments();
 AsyncCallback callback = (AsyncCallback)
args[args.length - 1];
 callback.onSuccess(mockServerAnswer);
 return mockRequest;
}

Having a Request return type for AsyncMethods has the advantage, that
you can use request.cancel() anyways, which sometimes comes in
handy... And of course, you should use dependency injection, as
mentioned above, i.e. never call GWT.create(MyService.class), but
instead use Gin and write the constructor as

@Inject
public MyWidget(MyServiceAsync service){
this.service = service;
}

or push the async service into the constructor by hand/factory, so you
can use a mock service with Mockito in your tests...

Greetz,
Peter


On Aug 13, 6:12 pm, pete  wrote:
> I always though this produced nicely readable tests for async calls:
>
> http://blog.reflectedcircle.co.uk/2009/12/08/gwt-rpc-asynccallback-te...
>
> Greetz
>
> On Aug 12, 11:24 am, Magno Machado  wrote:
>
>
>
>
>
>
>
> > Code from a @Test method:
> > List pesquisas = new ArrayList();
> >  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  Answer ok(final T result) {
> > return new Answer() {
>
> > @Override
> > public T answer(InvocationOnMock invocation) throws Throwable {
> > Object[] args = invocation.getArguments();
> > Object _receiver = args[args.length - 1];
> > Receiver receiver = (Receiver)_receiver;
> > receiver.onSuccess(result);
> > return null;
>
> > }
> >  };
> > }
> > On Thu, Aug 11, 2011 at 6:32 PM, objectuser  wrote:
> > > Here's how I do it.
> > >     @Test
> > >     public void testAsync() {
> > >         doAnswer(new Answer() {
>
> > >             @Override
> > >             public Void answer(InvocationOnMock invocation) throws
> > > Throwable {
> > >                 AsyncCallback callback =
> > > (AsyncCallback) 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.



Re: How to use Mockito for testing Async calls?

2011-08-13 Thread pete
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  wrote:
> Code from a @Test method:
> List pesquisas = new ArrayList();
>  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  Answer ok(final T result) {
> return new Answer() {
>
> @Override
> public T answer(InvocationOnMock invocation) throws Throwable {
> Object[] args = invocation.getArguments();
> Object _receiver = args[args.length - 1];
> Receiver receiver = (Receiver)_receiver;
> receiver.onSuccess(result);
> return null;
>
>
>
>
>
>
>
>
>
> }
>  };
> }
> On Thu, Aug 11, 2011 at 6:32 PM, objectuser  wrote:
> > Here's how I do it.
> >     @Test
> >     public void testAsync() {
> >         doAnswer(new Answer() {
>
> >             @Override
> >             public Void answer(InvocationOnMock invocation) throws
> > Throwable {
> >                 AsyncCallback callback =
> > (AsyncCallback) 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.



Re: How to use Mockito for testing Async calls?

2011-08-12 Thread Magno Machado
Code from a @Test method:
List pesquisas = new ArrayList();
 Request request = mock(Request.class);
doReturn(request).when(pesquisaRequest).listAll();
doReturn(pesquisaRequest).when(requestFactory).pesquisaRequest();
doAnswer(RequestFactoryUtils.ok(pesquisas)).when(request).fire(RequestFactoryUtils.anyReceiver());

And here my RequestFactoryUtils.ok:
public static  Answer ok(final T result) {
return new Answer() {

@Override
public T answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
Object _receiver = args[args.length - 1];
Receiver receiver = (Receiver)_receiver;
receiver.onSuccess(result);
return null;
}
 };
}

On Thu, Aug 11, 2011 at 6:32 PM, objectuser  wrote:

> Here's how I do it.
> @Test
> public void testAsync() {
> doAnswer(new Answer() {
>
> @Override
> public Void answer(InvocationOnMock invocation) throws
> Throwable {
> AsyncCallback callback =
> (AsyncCallback) 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 Paulo
http://blog.magnomachado.com.br
http://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.



Re: How to use Mockito for testing Async calls?

2011-08-11 Thread objectuser
 

Here's how I do it.
@Test
public void testAsync() {
doAnswer(new Answer() { 

@Override 
public Void answer(InvocationOnMock invocation) throws Throwable 
{ 
AsyncCallback callback = 
(AsyncCallback) 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.



Re: How to use Mockito for testing Async calls?

2011-08-11 Thread Juan Pablo Gardella
you can see gwt-dispatch  for
command pattern.

2011/8/11 Miroslav Genov 

> In my experience, using of mocking frameworks for testing of Async calls is
> not so good and tests are becoming un-readable at some point of time. In our
> app we are using Command Pattern to send different requests
> (Action/Response) and here is our helper class that we are using for
> testing:
>
> public class FakeRpcService implements RpcServiceAsync {
>
>   public Action lastAction;
>   public AsyncCallback lastCallback;
>
>   public  void execute(Action action,
> AsyncCallback async) {
> lastAction = action;
> lastCallback = async;
>   }
>
>   public  T lastAction() {
> return (T) lastAction;
>   }
>
>   public void responsesWith(Response response) {
> // SMELL private members of simple object are referencing template
> members?
> ((AsyncCallback) lastCallback).onSuccess(response);
>   }
>
>   public void failsWith(RuntimeException e) {
> ((AsyncCallback) lastCallback).onFailure(e);
>   }
> }
>
> so our tests now are looking like:
>
>  @Test
>   public void exportSelectedInvoices() {
> List invoices =
> Lists.newArrayList(createInvoice("1",PaymentType.CASH));
>
> // filter button was clicked
> presenter.onReportRequested();
> service.responsesWith(new GetInvoicesPeriodReportResponse(invoices));
>
> presenter.onExportSelectedInvoices(new HashSet());
>
> GenerateAjurExportAction action = service.lastAction();
> assertThat("the one selected invoice was not sent for
> export?",action.getInvoices(), is(equalTo(invoices)));
>
> service.responsesWith(new GenerateAjurExportResponse("test test"));
> assertThat("export response was not
> displayed?",display.exportResponse,is(equalTo("test test")));
>   }
>
> Hope this will help you figure out what's the best choice for you.
>
>  --
> 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/-/OTxCnyHee14J.
>
> 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.
>

-- 
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.



Re: How to use Mockito for testing Async calls?

2011-08-11 Thread Miroslav Genov
In my experience, using of mocking frameworks for testing of Async calls is 
not so good and tests are becoming un-readable at some point of time. In our 
app we are using Command Pattern to send different requests 
(Action/Response) and here is our helper class that we are using for 
testing: 

public class FakeRpcService implements RpcServiceAsync {

  public Action lastAction;
  public AsyncCallback lastCallback;

  public  void execute(Action action, 
AsyncCallback async) {
lastAction = action;
lastCallback = async;
  }

  public  T lastAction() {
return (T) lastAction;
  }

  public void responsesWith(Response response) {
// SMELL private members of simple object are referencing template 
members?
((AsyncCallback) lastCallback).onSuccess(response);
  }

  public void failsWith(RuntimeException e) {
((AsyncCallback) lastCallback).onFailure(e);
  }
}

so our tests now are looking like:

 @Test
  public void exportSelectedInvoices() {
List invoices = 
Lists.newArrayList(createInvoice("1",PaymentType.CASH));

// filter button was clicked
presenter.onReportRequested();
service.responsesWith(new GetInvoicesPeriodReportResponse(invoices));

presenter.onExportSelectedInvoices(new HashSet());

GenerateAjurExportAction action = service.lastAction();
assertThat("the one selected invoice was not sent for 
export?",action.getInvoices(), is(equalTo(invoices)));

service.responsesWith(new GenerateAjurExportResponse("test test"));
assertThat("export response was not 
displayed?",display.exportResponse,is(equalTo("test test")));
  }

Hope this will help you figure out what's the best choice for you.

-- 
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/-/OTxCnyHee14J.
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.



Re: How to use Mockito for testing Async calls?

2011-08-11 Thread Juan Pablo Gardella
One solution is refactoring your code and where you have  GreetingService as
an object injected by gin

2011/8/11 yogi 

> Hi all,
> I want to know how to use Mockito for testing Async calls. I went
> through few blogs, and I am not sure it is implemented and how it
> runs. For instance, if I have an server implementation as:
> public class GreetingServiceImpl extends RemoteServiceServlet
> implements GreetingService {
>
>public String greetServer(String input) {
>return "Hello, " + input;
>}
> }
>
> And in the client code I write
> GreetingServiceAsync greetingServer =
> GWT.create(GreetinService.class);
> greetingServer.greetServer("World", new AsyncCallback() {
> ...
> }
> });
>
> How do I exactly test this using Mockito? Any code snippet you can
> share?
> Thanks a lot.
>
> --
> 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.
>
>

-- 
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.



How to use Mockito for testing Async calls?

2011-08-11 Thread yogi
Hi all,
I want to know how to use Mockito for testing Async calls. I went
through few blogs, and I am not sure it is implemented and how it
runs. For instance, if I have an server implementation as:
public class GreetingServiceImpl extends RemoteServiceServlet
implements GreetingService {

public String greetServer(String input) {
return "Hello, " + input;
}
}

And in the client code I write
GreetingServiceAsync greetingServer =
GWT.create(GreetinService.class);
greetingServer.greetServer("World", new AsyncCallback() {
...
}
});

How do I exactly test this using Mockito? Any code snippet you can
share?
Thanks a lot.

-- 
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.