4

I have an GWT application, with few servlets on the server side. I would like to test those servlets (without the need to make GUI tests with Selenium, or any other web-based framework). Or in other words I want the test to simulate the client side of GWT.

The natural challenges with testing the servlets are:

  1. Starting the webserver,
  2. Simulation of client,
  3. Servlets return immediately, passing the value to AsyncCallback object.

So far, I've been able to figure out (although this is still not tested), that: 1. I can start the container by extending GWTTestCase 3. I have found a google doc about asynchronous testing, so it is possible to wait for the async callback. Google docs are also mentioning this:

Server side testing

The tests described above are intended to assist with testing client side code. The test case wrapper GWTTestCase will launch either a development mode session or a web browser to test the generated JavaScript. On the other hand, server side code runs as native Java in a JVM without being translated to JavaScript, so it is not necessary to run tests of server side code using GWTTestCase as the base class for your tests. Instead, use JUnit's TestCase and other related classes directly when writing tests for your application's server side code. That said, you may want both GWTTestCase and TestCase coverage of code that will be used on both the client and the server.

But there are no examples or more in-depth explanation how to achieve this.

I haven't figured out how to simulate the client... Any ideas how can I do that?

Or, if this is not the way to do this, is there any other way? I would prefer to use native GWT classes, not not some 3rd party frameworks for testing the servlets.

Thanks!

damwas
  • 41
  • 5

1 Answers1

4

How about using an embedded Jetty instance... The Jetty server is inculded in the GWT SDK anyway. So just include the gwt-dev.jar in your project and there you go for the server-side. Emulating the client-side is a whole different story. The problem is the JavaScript to Java serialization/deserialization which happens in GWT magic....

There is a project called gwt-syncproxy that can help here: http://code.google.com/p/gwt-syncproxy/

In code this could look like this:

import junit.framework.Assert;

import org.junit.BeforeClass;
import org.junit.Test;

import org.mortbay.jetty.Server;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.ServletHolder;

import com.gdevelop.gwt.syncrpc.SyncProxy;

public class ServletTest {

    private Server _server;

    @BeforeClass
    public void setUp() throws Exception {
        _server = new Server(8080);
        Context root = new Context(_server, "/", Context.SESSIONS);
        root.addServlet(new ServletHolder(new MyServiceImpl()), "/servlet");
        _server.start();
    }

    @Test
    public void testMethod1() throws Exception {
        MyService rpcService = (MyService) SyncProxy.newProxyInstance(MyService.class, "http://127.0.0.1:8080/servlet", "testMethod1");

        String result = rpcService.testMethod1();

        Assert.assertTrue(result != null);
    }

}
Adrian B.
  • 4,333
  • 1
  • 24
  • 38