gwtXPDownloadsBlog

Testing GWT RPC services

Posted in GWT on January 10th, 2010 7 Comments

You may know that testing with GWTTestCase are very slow. And there are many posts about testing GWT applications without using GWTTestCase.

However, it is still hard to test our RPC remote services. That is the reason I decided to develop SyncProxy which can run directly from pure Java (non JSNI) code.

Simple Test case

For example, we have an helloApp application and we want to test our GreetingService

@RemoteServiceRelativePath("greet")
public interface GreetingService extends RemoteService {
  String greetServer(String name);
}

Assume the server side of application is running (whether in DevMode or deployed to an web server) and the servlet URL is configured at http://localhost/helloApp/greet

The test case

public class GreetingServiceTest extends TestCase{
  private static GreetingService rpcService =
    SyncProxy.newProxyInstance(GreetingService.class,
          "http://localhost/helloApp", "greet");

  public void testGreeting() {
    String result = rpcService.greetServer("SyncProxy");
    assertTrue((result != null) && (result.startsWith("Hello, SyncProxy")))
  }
}

Explanation

SyncProxy.newProxyInstance() method requires a service interface class, a base URL and a relative servlet path.

SyncProxy will search for RPC policy files (*.gwt.rpc files) to determine appropriate policy name for the service interface. Thus, we copy gwt.rpc files from war/helloApp directory to our test case classpath.

SyncProxy.newProxyInstance() will return a new proxy instance which implements our GreetingService interface.

Simulating Async

By design SyncProxy is synchronous, e.g it invoke the remote service and wait for the result. However, our GWT logic code invokes the remote service asynchronously. SyncProxy can simulate the ‘Async’ mode too.

GreetingServiceAsync rpcServiceAsync =
SyncProxy.newProxyInstance(GreetingServiceAsync.class,
          "http://localhost/helloApp", "greet");

The code is almost the same as section above, except we use the Async version of the remote service interface.

rpcService.greetServer("SyncProxy", new AsyncCallback<String>(){
  public void onFailure(Throwable caught) {
    ...
  }
  public void onSuccess(String result) {
    ...
  }
});

Download SyncProxy

Get SyncProxy (with source code) at our Downloads page

SyncProxy includes test suite (see the Java source file com.gdevelop.gwt.syncrpc.test.RPCSyncSuite) to test against the standard GTW RPC test.

Updated on 2010/03/03: Version 0.1.1 is released with cookie supports

Updated on 2010/03/13: New post Invoke GWT RPC services deployed on Google App Engine.

Share and Enjoy:

  • Google Bookmarks
  • Twitter
  • Facebook
  • Digg
  • Technorati
  • Live
  • DZone
  • Reddit
  • del.icio.us
  • Mixx
  • Netvibes
  • StumbleUpon
  • Yahoo! Bookmarks
  • Yahoo! Buzz
  • email
Leave a Comment »

7 Responses to “Testing GWT RPC services”

  1. Ron says:

    Hi, I downloaded syncproxy and the jar file appears to be corrupted.

  2. Trung says:

    Hi Ron,

    Thanks for your feedback. I sent the SyncProxy source code to you. Please check your mail box.

    And please tell us what’s wrong with the jar file? Your information is useful for us to prevent the problem for other readers :)

  3. Ed says:

    Will this only test your code locally?
    I am trying to built a test suite which I can run monthly on my deployed app. This suite should help me find out if there are any issues. Could this help?

    Thanks.

  4. Trung says:

    Hi Ed,

    Of course, SyncProxy can be used to test the deployed app.

    For example, testing the deployed helloApp is as below:

    // Create new proxy instance for the helloApp deployed on http://www.example.com
    GreetingService rpcService =
    SyncProxy.newProxyInstance(GreetingService.class,
    “http://www.example.com/helloApp”, “greet”);

    Hope this help.

  5. Ed says:

    Awesome! Thanks. I’ll try this today!

  6. Ed says:

    One more thing. Sorry to keep bothering. Will this work with http sessions?

  7. Trung says:

    Hi Ed,

    Thanks for your questions. It suggests me a new feature of SyncProxy.

    I just released a new version of GWT SyncProxy which supports cookies and therefore supports http sessions.

    Checkout the SyncProxy 0.1.1 at the download page.

    Please note, the source code is included in the jar file, you can modify it to meet your requirements.

    Cheers.

Leave a Reply



Recent Comments:

  • Trung: There are no Benchmarks now. However, GWT RPC is NOT a generic, language-independent...
  • Jeff Schnitzer: This is very cool! Any metrics on how it compares, performancewise, to Hessian,...
  • Trung: Hi Ed, Thanks for your questions. It suggests me a new feature of SyncProxy. I just...
  • Ed: One more thing. Sorry to keep bothering. Will this work with http sessions?
  • Ed: Awesome! Thanks. I’ll try this today!