Unit Tests in GWT (using Maven and eclipse PDE/OSGi)

One of the strengths of GWT, from my point of view, is that large portions of the code (eg business logic) can be tested with plain old JUnit tests, without the slightest interference from servers, web browsers and other constraints.

However, certain parts of the code must be tested in a richer context including the compiled java script or client/server communication. Luckily, the GWTTestCase provides a powerful tool to automate tests in such rich contexts.

Following a few pointers, of how these GWT unit tests can be setup in an environment using Maven and the eclipse PDE (the tests are not exactly using this technology but should be able to be part of a possible OSGi module).

  • Create a new gwt module (gwt.xml) in a dedicated package (eg gwttests) in the src/test/java folder such as the following. Make this unit inherit the modules, you want to test.



    <!– Inherit the core Web Toolkit stuff. –>



    <!– Other module inherits –>




    <!– Specify the app entry point class. –>



    <!– Specify the paths for translatable code –>







  • Create a class in that package following the naming pattern: “GwtTest*.java”, let this class extend com.google.gwt.junit.client.GWTTestCase. Return a module name pointing to your module (gwt.xml file) for gwtModuleName()

class GwtTestClientServerCommunication extends GWTTestCase {



    public String getModuleName() {




void testNetworkOperations() {

        Network n = Nx.newNetwork();

        String root = “root”;


        Nx.append().node(“a string”).to(root).inNetwork(n);






  • Now you should be able to run this test using “mvn gwt:test” (but make sure to check for the pitfalls below).

Some possible pitfalls

-No compilation unit found for test-

The GWT compiler might report that the class of the test case could not be found:

[INFO] com.google.gwt.junit.JUnitFatalLaunchException: The test class …’ was not found in module …; no compilation unit for that type was seen

In this case, there might be problem with the module (gwt.xml) file you have defined, for instance you forgot to declare “<source


-Need to manually open test case in browser-

When executing gwt:test, the maven gwt plugin (or the gwt compile) might report the following:

[INFO] Please navigate your browser to this URL:

[INFO] http://130.216.XX.XX:54530/&#8230;.JUnit/junit.html?gwt.codesvr=130.216.XX.XX:54526

This can be omitted by configuring the maven gwt plugin to utilize htmlunit to lunch a browser. Add the following to the pom.xml:








Now the tests should run without the need to open a browser manually (see Gwt Maven Plugin Parameters Mode)


-Access to junit.framework in eclipse PDE-

Eclipse PDE might report that: “Access restriction: The method assertTrue(boolean) from the type Assert is not accessible due to restriction on required library”

This can mitigated by adding the following dependency:







As well as the following imported package in the configuration of the maven bnd plugin:




After rebuilding the project with “mvn eclipse:clean clean package eclipse:eclipse -Declipse.pde -Declipse.useProjectReferences=false install” and refreshing the files in eclipse, the access restriction error should disappear.


-Calls to server Servlets result in 404 errors-

Some calls per RPC to Servlets on the server might result in 404 errors. In your original project, the Servlets could just be declared in the web.xml configuration. In order for Servlets to work in the GWT unit test, they must be declared in the gwt module definition (and here it is sensible to define them in the module you want to test rather than the ‘test’ module).

<servlet path=’/yourservletforrpc’ class=’com.yourpackage.Servlet’></servlet>



Gwt Maven Plugin Parameters Mode (set this to htmlunit)

Maven Gwt Plugin User Guide: Testing

GWT Developer’s Guide: JUnit Testing


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s