Thursday, January 28, 2010

Problems with Unit Testing using GWTTestCase and GWT FlexTable()

I'm currently writing a web-application using Google Web Toolkit (GWT). To provide test-coverage for the app, I'm writing client-side unit tests that extend GWTTestCase.

Recently I found that some (previously passing) tests were failing. They threw a JavaScript Exception (com.google.gwt.core.client.JavaScriptException (null) null):

java.lang.RuntimeException: Remote test failed at 192.168.1.1 / Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1
at com.google.gwt.junit.JUnitShell.processTestResult(JUnitShell.java:1083)
at com.google.gwt.junit.JUnitShell.runTestImpl(JUnitShell.java:1203)
at com.google.gwt.junit.JUnitShell.runTestImpl(JUnitShell.java:1198)
at com.google.gwt.junit.JUnitShell.runTestImpl(JUnitShell.java:1198)
at com.google.gwt.junit.JUnitShell.runTestImpl(JUnitShell.java:1104)
at com.google.gwt.junit.JUnitShell.runTest(JUnitShell.java:523)
at com.google.gwt.junit.client.GWTTestCase.runTest(GWTTestCase.java:406)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at com.google.gwt.junit.client.GWTTestCase.run(GWTTestCase.java:282)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:76)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: com.google.gwt.core.client.JavaScriptException: (null): null
at
...



The tests were failing on a GWT constructor call designed to create a new FlexTable:

FlexTable flexTable = new FlexTable();

Confused about what could be going on, I created a simple test which just called this constructor and found that it failed:

package test.client;

import com.google.gwt.junit.client.GWTTestCase;

import com.google.gwt.user.client.ui.FlexTable;

public class FlexTableTest extends GWTTestCase {

public void testFlexTable() {

FlexTable flexTable = new FlexTable();

}

@Override

public String getModuleName() {

return "test.FlexTableTester";

}

}


So - I created a completely new Project package from scratch and then found it passed just fine.

So - what was going on? Well it turns it was to do with my "myProject.gwt.xml" file. It seems that when you run the GWT tests using GWTTestCase, the code is compiled and run on a virtual browser - that may be different from the actual browser you're using to run the application in for manual testing or acceptance tests. Obvious, in retrospect!

Now, in my project, I had decided that since I was only running the application (and Selenium acceptance tests) in Internet Explorer browsers, I could save GWT compile time by only compiling one permutation of the GWT options - that for Internet Explorer.

To do this, I had introduced the following line of code into my "myProject.gwt.xml" file:

<set-property name="user.agent" value="ie6"/>

This code tells GWT to only compile one permutation - for Internet Explorer. However, it seems that the "virtual browser" used by GWTTestCase is not InternetExplorer (or at least not fully compatible with ie6 mode).

Having this property set to a particular browser (IE6) led to the problems I was having with my tests.

All I had to do was remove the <set-poroperty name="user.agent" value="ie6"/> tag and the tests worked perfectly.

1 comment:

Anonymous said...

Hi!

I'm facing the same issue while running unit tests in NetBeans. But I don't have any user agents in module file. May it be a problem in something else?

Anton
(http://anton.troshin.name)