Wednesday, March 18, 2009

Running GWTTestSuite in JUnit 4.4 without the ClassCastException

Running unit tests using the Google Web Toolkit that extends GWTTestCase can be very slow and time consuming, since the JUnitShell has to be restarted for each test. You can speed up testing a lot by instead using GWTTestSuite to group these tests into a single suite than only needs to start the JUnitShell once. The developer's guide gives the following example:
public class MapsTestSuite extends GWTTestSuite {
public static Test suite() {
TestSuite suite = new TestSuite("Test for a Maps Application");
suite.addTestSuite(MapTest.class);
suite.addTestSuite(EventTest.class);
suite.addTestSuite(CopyTest.class);
return suite;
}
}
However, running this using the junit.testui.TestRunner in JUnit4.4 (for example if you are running from the command line using the Maven GWT plugin) gives the following helpful error:

Error: java.lang.ClassCastException


Instead you want to do the following:
public class MapsTestSuite extends TestCase {
public static Test suite() {
GWTTestSuite suite = new GWTTestSuite("Test for a Maps Application");
suite.addTestSuite(MapTest.class);
suite.addTestSuite(EventTest.class);
suite.addTestSuite(CopyTest.class);
return suite;
}
}


i.e. Extend TestCase rather than GWTTestSuite to overcome this problem.