2011-04-27

GWT testing using Maven and Eclipse

When developing an application using Google Web Toolkit, it's a good practice to have both unit tests which do not require GWT and GWT tests for custom widgets or integration.
If you use Maven, you should apply the default naming convention of the Maven GWT plugin : *Test.java for standard unit tests and GwtTest*.java for GWT test cases. This way you have nothing to configure in your pom.xml.

Then you'll get errors from the GWT compiler because your unit tests are located in same java packages as your GWT application code but in a different source directory (e.g. src/test/java) and the compiler cannot find their source files. While these errors are not fatal and could be ignored, they are annoying. You can get rid of them by excluding your unit test classes by file pattern in your .gwt.xml file like below:


<source path='client' excludes="**/*Test.java,**/Mock*.java" />
<source path='shared' excludes="**/*Test.java" />

When you will run your GWT application, you may see error messages like:
[ERROR] [MyApp] - Line 13: No source code is available for type com.google.gwt.junit.client.GWTTestCase; did you forget to inherit a required module?

It does not prevent your application from running but they are annoying.
You can get rid of them by moving your GWT test cases to a dedicated source directory that you will exclude from the classpath of your Eclipse run configuration.
Finally, you'll put your standard unit tests under "src/test/java" and your GWT tests under "src/gwt-test/java". In your pom.xml, you have to use the build-helper-plugin to add this extra test directory.


<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/gwt-test/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>


If you want to speed up your GWT tests, you can launch them from a GWT test suite but then you must configure the Maven plugin for running only your test suite otherwise you'd end up running same test twice. My naming convention is Gwt*Suite.java which works well with the two other conventions.



<plugin>
<groupid>org.codehaus.mojo</groupid>
<artifactid>gwt-maven-plugin</artifactid>
<version>2.2.0</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- Run only test suites not individual test cases. -->
<includes>**/Gwt*Suite.java</includes>
</configuration>
</plugin>

1 commentaire:

Matteo Traina a dit…

Thanks a lot, very useful tip.