2
votes


has someone here experiences with GWT SyncProxy?
I try to test an asynchronous rpc, but the code under onFailure and onSuccess is not tested. Unfortunately there is no error log, but maybe someone can help me. The example is from this page: http://code.google.com/p/gwt-syncproxy/

Edit:
I want that the test fails. So i added 'assertNull(result);'. The strange thing is that the console gives as result first 'async good' and after that 'async bad'. So the function is running twice?! And Junit gives as result green.

public class Greeet extends TestCase {
@Test
public void testGreetingServiceAsync() throws Exception {
      GreetingServiceAsync rpcServiceAsync = (GreetingServiceAsync) SyncProxy.newProxyInstance(
        GreetingServiceAsync.class, 
        "http://127.0.0.1:8888/greettest/", "greet");

      rpcServiceAsync.greetServer("SyncProxy", new AsyncCallback<String>() {
        public void onFailure(Throwable caught) {
          System.out.println("Async bad " );
        }
        public void onSuccess(String result) {
          System.out.println("Async good " );
          assertNull(result);
        }
      });

      Thread.sleep(100); // configure a sleep time similar to the time spend by the request
}
}
1

1 Answers

1
votes

In order to test with gwt-syncproxy:

  1. You have to start the gwt server: 'mvn gwt:run' if you are using maven or 'project -> run as -> web app' in the case you are in eclipse.
  2. You have to set the url of the service, normally 'http://127.0.0.1:8888/your_module'. Note that in your example you are using the url of the application html.
  3. If you test async, you have to wait until the call finishes, so in your case you need a Thread.sleep(sometime) at the end of your method.
  4. If you test sync, the sleep is not needed.

This are two example test cases:

Synchronous Test

public void testGreetingServiceSync() throws Exception {
  GreetingService rpcService = (GreetingService)SyncProxy.newProxyInstance(
     GreetingService.class, 
    "http://127.0.0.1:8888/rpcsample/", "greet");
  String s = rpcService.greetServer("SyncProxy");
  System.out.println("Sync good " + s);
}

Asynchronous Test

boolean finishOk = false;
public void testGreetingServiceAsync() throws Exception {
  GreetingServiceAsync rpcServiceAsync = (GreetingServiceAsync) SyncProxy.newProxyInstance(
    GreetingServiceAsync.class, 
    "http://127.0.0.1:8888/rpcsample/", "greet");

  finishOk = false;
  rpcServiceAsync.greetServer("SyncProxy", new AsyncCallback<String>() {
    public void onFailure(Throwable caught) {
      caught.printStackTrace();
    }

    public void onSuccess(String result) {
      assertNull(result);
      finishOk = true;
    }
  });

  Thread.sleep(100);
  assertTrue("A timeout or error happenned in onSuccess", finishOk);
}