Posts tagged ‘Tutorial’

June 18, 2011

Tutorial GWT Request Factory – Part II

by Stefan

In the first part of the tutorial we set up EntityProxy classes for our back-end entities pizza and ingredient. A PizzaRequestContext was introduced that represents the client-side facade for the PizzaDao in the back-end.

Now, a natural next step is to write some kind of controller logic that uses the PizzaRequestContext to communicate with the back-end. Let’s call this controller PizzaManager:

package cleancodematters.client;


public class PizzaManager {

  private final PizzaRequestFactory factory;

  public PizzaManager( PizzaRequestFactory factory ) {
    this.factory = factory;

  public void findById( Long id, Receiver<PizzaProxy> receiver ) {
    factory.context().findById( id ).with( "ingredients" ).fire( receiver );

The manager gets a RequestFactory instance passed into the constructor. This is a good idea as creating the RequestFactory requires a GWT#create() call which doesn’t work in plain JUnit tests. See my previous post on how to use GIN get the instance injected automatically.

How can we test the implementation of findById() with plain JUnit tests? One approach is to use a mocked PizzaRequestFactory instance. In our test we then have to ensure that the method chain factory.context().findById( id ).with( "ingredients" ).fire( receiver ) is called correctly. This test code is hard to write and also tied very closely with implementation details. In general, fluent interfaces are nice to read (but often violate the Law of Demeter, btw) but testing this code with mocks can be really cumbersome.

A better approach in my view is to use GWT’s RequestFactory infrastructure and replace the transport layer with some “in memory” processing that is independent of the browser infrastructure. Fortunately, GWT already provides a class for this: InProcessRequestTransport. This approach has another advantage: We also test the error-prone reference of nested entities (with( "ingredients" ) in the example).

read more »

June 4, 2011

Tutorial GWT Request Factory – Part I

by Stefan

Update 30.12.2012: All examples are reviewed and updated to GWT 2.5. Enjoy!
Update 24.09.2011:
I finally managed to push the complete tutorial project to github. It is based on GWT 2.4. 2.5. Looking forward to your feedback.

The first part of this tutorial describes how to set up a Request Factory (RF) based client-server communication in GWT. In contrast to the example at GWT home, a classic DAO pattern is used at the server-side with a clear separation between entity (passive, stateful) and dao/service (active, stateless). As this is not the default way, some additional helper classes (“locators”) need to be implemented. However, I think the benefit of a cleaner architecture is worth the price of some additional lines of code.

The second part will deal with testing RF-based classes in the GWT client.


You should have the GWT SDK installed and a GWT compatible project in the IDE of your choice available. To make the example run you need some additional libraries on the classpath:

  • org.json.*
  • a JSR 303 Bean Validation implementation, e.g. hibernate-validator

When using Maven, simply add these dependencies:


<!-- Validation API -->

<!-- Validation Implementation -->

<!-- Need some logging provider for SLF4J -->

Without Maven you can use gwt-servlet-deps.jar from the GWT SDK (containing the JSON packages) and download the hibernate-validator jar with its dependencies.

read more »