Summary
In his latest IBM developerWorks article, Andrew Glover demonstrates GWTTestCase, the Google Web Toolkit's test helper class, and shows how to test asynchronous Ajax applications with GWT.
Advertisement
Testing Ajax applications has been a difficult task: Not only are such applications an amalgamation of a variety of technologies, such as HTML, CSS, JavaScript, and server-side code, but Ajax's asynchronous messaging paradigm adds more complexity to testing.
In his recent IBM developerWorks article, Unit testing Ajax applications, Andrew Glover focuses on testing techniques for Ajax applications written with the Google Web Toolkit (GWT). GWT allows developers to write Ajax applications in Java, and a Java-to-JavaScript compiler then translates Java code to appropriate JavaScript.
One advantage of this approach is better testability: Because GWT applications are written in Java, Java testing tools can be used to for GWT application testing. Glover notes that,
The GWT development team created a helper class dubbed GWTTestCase that extends from JUnit's 3.8.1 TestCase. This base class adds functionality for testing GWT code as well as handling some of the plumbing to get a GWT component up and running... it's meant to facilitate testing asynchronous aspects that can be triggered by UI interactions...
User interaction testing isn't possible with the GWTTestCase test case environment. When designing and building GWT applications, you must think about testing the code without relying on user interactions. This sort of thinking requires a decoupling of interaction code from business logic...
Glover highlights a few coding conventions to enable testability of GWT applications:
GWTTestCase requires you to play by some rules if you want to benefit from its testing magic. Luckily, the rules are simple:
All implementing test classes must reside in the same package as the GWT module you're attempting to test.
When running tests, you must pass in at least one VM argument to help specify which GWT mode (hosted or Web) to run the test in.
You must implement the getModuleName() method, which returns a String representation of your XML module file.
According to Glover, the GWT testing classes aid in unit testing asynchronous Ajax code as well:
Because an Ajax application that communicates with a server-side entity is asynchronous by nature, the GWT offers an additional Timer class that facilitates delaying JUnit long enough for asynchronous behavior to complete before an associated assertion...
[You'll] have to force JUnit to delay running to completion when [you] call ... a method via my test case; otherwise, the test will fail due to no response. This is where GWT's Timer class comes in handy. Timer allows me to override [a method] and finish the test case's logic within Timer instead.
What do you think of Ajax application testing? How would you compare the state of Ajax testing tools to Java testing tools?