Better Software - November 2008 - (Page 27) R achel had successfully built her Web 2.0 Ajax application using Google Web Toolkit, and her boss and customers were ecstatic. Revenue had increased, which led to a steady stream of new business requirements. But there was a catch: Bugs appeared, got fixed, and mysteriously reappeared. Rachel found her team spending an increasing amount of time fixing bugs while new feature development slowed to a crawl. She remembered that test-driven development was a great way to build applications in a manner that prevented defects from reappearing. She knew that Google Web Toolkit had been built with testability in mind, so Rachel downloaded JUnit and started writing tests In the October 2008 issue of Better directly in JavaScript, as indicated by method called getModuleName, which Software magazine, part one of this se- the keyword “native” in the definition returns a string containing the name of ries introduced the Google Web Toolkit of getCellElement. As shown in listing your GWT code module as defined in (GWT), a tool for building cross-browser 1, many of the GWT libraries include your application’s module configuration Ajax applications written in Java. GWT some native code—in particular, all wid- XML file. (GWT applications can be compiles Java code into JavaScript and gets that manipulate the Domain Object grouped into reusable modules, each of provides a component library that is Model (DOM). Thus, when your unit which contains an XML file descriptor cross-browser compatible and memory- tests execute native JavaScript, you must with information including source code leak proof. This means that you can be running in an environment where it location, dependencies, target browsers, focus on writing your application busi- can be executed, such as the hosted- etc.) ness logic instead of handling the acci- mode browser provided by GWT. When you run your test, the GWT dental complexity of supporting multiple To test native JavaScript code, GWT framework starts up an invisible (or browsers. provides a subclass of JUnit’s TestCase “headless”), hosted-mode browser and Another core feature of public void setStylePrimaryName(int row, int column, String styleName) { GWT is testability, which UIObject.setStylePrimaryName(getCellElement(bodyElem, row, column), means it’s easy to unit test styleName); your application. This makes } it possible to write GWT applications test first—an agile private native Element getCellElement(Element table, int row, int col) /*-{ practice that helps build relivar out = table.rows[row].cells[col]; able and extensible applicareturn (out == null ? null : out); tions. This article introduces }-*/; GWT’s testing infrastructure and demonstrates how to Listing 1 build an Ajax application test first. public class MeetingSummaryLabelTest extends GWTTestCase { gwt’s testing infrastructure Since a GWT application is almost entirely written in Java, you can test almost all of it using standard JUnit tests. However, GWT also includes a special subclass of JUnit’s TestCase that can test code that requires JavaScript at run time. While all of your client-side Java code will ultimately be compiled to JavaScript, only some of it directly uses code implemented as JavaScript. For example, the code in listing 1 is from the GWT HTMLTable class. This code sample demonstrates a method written in Java (setStylePrimaryName) that relies on code implemented public String getModuleName() { return “com.danielwellman.booking.Booking”; } // Add tests here } Listing 2 called GWTTestCase. This base class allows you to implement your JUnit test case as you normally would. In fact, GWTTestCases look almost identical to the standard JUnit TestCase shown in listing 2. The only visible difference is that all GWTTestCases must override an abstract www.StickyMinds.com then evaluates your test case against it. What this means is that all the facilities of the hosted browser are available to your test case. You can run native JavaScript functions, render widgets, or invoke asynchronous remote procedure calls. Furthermore, you can run your tests either as a hybrid of Java and JavaScript NOVEMBER 2008 BETTER SOFTWARE 27 http://www.nxtbook.com/nxtbooks/sqe/bettersoftware1008/index.php?startid=36 http://www.nxtbook.com/nxtbooks/sqe/bettersoftware1008/index.php?startid=36 http://www.StickyMinds.com
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.