Friday, July 06, 2007

Creating a GwtTestCase under eclipse

Creating a new JUnit test case with GWT integration - and having it work with Eclipse.

Perhaps you have a project structure that's different than what the GWT tools provide for you automatically? Perhaps you like to know what's going on under the hood? Either way, these steps should illuminate a little bit about how GWT wants you to invoke its GWTTestCase tests.

Steps:
- Create a test folder, and mirror the "client" package name you have under src. Eg: src/com/goodstuff/client/ would have an associated test/com/goodstuff/client/ directory for tests. The tests will not run successfully if you use a different package name.

- Create a test case within this package. Have it extend from the com.google.gwt.junit.client.GWTTestCase class.

- Implement the getModuleName() method. More than likely this will be the same for all your tests. This will be the Module Name of your main application. For me, it was "davefoolery.gwtTest.TestTwoApp". A 'module' has a corresponding file ending in .gwt.xml, so you'll know you have the right module name if the module com.yourcompany.FancyApp has an associated com/yourcompany/FancyApp.gwt.xml file.

- Create a run configuration for this test - easiest way to do that is to go run->run as JUnit Test. This will fail horribly, don't worry - we're not done yet.

- Add the gwt-dev-*.jar to the classpath of the run configuration for this test. If you want (not recommended), you could also add it to the project wide configuration. * See note at end.

- Ensure both "src" and "test" sub directories are in the classpath configuration for this test - GWT needs these to find the various files we're pointing at (test .java files, etc).

- Ensure you don't use any Java 5 features in your GWTTestCase tests.

- Run your test - it should pass now.


*NOTE: The -dev*.jar files use different names under Linux/OSX/Windows, and so will cause your runtime configurations/project files to be unportable if a different platform author needs to import your .project/.classpath/... settings.