Section 8.13. JUnit Testing


8.13. JUnit Testing

What if you want to test your application? The GWT includes a way to test aspects of your Ajax application, including the GUI, using the well-known JUnit framework. This shortcut introduces these concepts by showing you how to set up a basic JUnit test for your dynamic Ajax graphical interface.

Here are the steps you can take to set up a JUnit test in GWT.

  1. Call the junitCreator shell script, provided by GWT when it built your application directory. Here is the syntax I used:

     junitCreator -junit /Users/bruceper/1ebooks/gwt/WEB-INF/lib/junit.jar -out /Users/bruceper/1ebooks/gwt com.parkerriver.gwt.intro.client.GwtTest 

    This command line created a folder in the top-level directory of my project named test, with the skeleton of a test-case class called GwtTest.java nested within that directory (meaning the script created all of the other necessary directories beneath test such as com and parkerriver). junitCreator also created two new shell scripts named GwtTest-hosted and GwtTest-web.

  2. Write your tests inside the test-case class, which we will show in a moment.

  3. Compile the test-case class.

  4. Make sure that a compiled version of your client-side GWT Java class (e.g., GwtAjax.java) and any of its dependencies are also placed with the test-case class, which will reference your GWT class.

  5. Make sure that this directory containing the compiled classes, such as /bin, is included in the classpath of the XXX-hosted script (e.g., GwtTest-hosted).

  6. Execute the XXX-hosted command-line script to see the results of your tests.

Here is what the XXX-hosted script looks like on my system.

 #!/bin/sh APPDIR='dirname $0'; java -XstartOnFirstThread -Dgwt.args="-out www-test" -cp "$APPDIR/src:$APPDIR/test:$APPDIR/bin:/Users/bruceperry/1ebooks/gwt/WEB- INF/lib/junit.jar:/Users/bruceperry/1gwt/gwt-mac-1.2.11/gwt- user.jar:/Users/bruceperry/1gwt/gwt-mac-1.2.11/gwt-dev-mac.jar" junit.textui.TestRunner com.parkerriver.gwt.intro.client.GwtTest "$@"; 

The following code shows the GwtTest test-case class. These classes must extend com.google.gwt.junit.client.GWTTestCase from the GWT API.

 package com.parkerriver.gwt.intro.client; import com.google.gwt.junit.client.GWTTestCase; import com.google.gwt.user.client.ui.*; /**  * GWT JUnit tests must extend GWTTestCase.  */ public class GwtTest extends GWTTestCase { /**  * Must refer to a valid module that sources this class.  */ public String getModuleName() {   return "com.parkerriver.gwt.intro.GwtAjax"; } public void testCreateTextBox(){  GwtAjax gwtaj = new GwtAjax();  assertNotNull(gwtaj);  gwtaj.onModuleLoad();  TextBox tb = gwtaj.createTextBox(40,40,               "tb",null);  assertNotNull(tb); } public void testGui(){   GwtAjax gwtaj = new GwtAjax();   assertNotNull(gwtaj);   gwtaj.onModuleLoad();   Grid grid = gwtaj.getFormGrid();   assertNotNull("Grid is not null.",grid);   Widget wid = grid.getWidget(3,0);   assertNotNull("Widget is not null.",wid);   Button button = (Button) wid;   assertTrue("Button label = request info.",               (button.getText().equalsIgnoreCase(                       "request info")));   //Click the button, etc.   button.click();  } } 

The test classes have a getModuleName() method that must return the fully qualified class name of your module. Compile this class and run the XXX-hosted script that junitCreator generated. If the test methods pass, you should see the output in your terminal window similar to Figure 11.

Figure 8-6. The output from running a JUnit test with GWT





Google Web Toolkit for Ajax
Google Web Toolkit GWT Java AJAX Programming: A step-by-step to Google Web Toolkit for creating Ajax applications fast
ISBN: 1847191002
EAN: 2147483647
Year: 2006
Pages: 29

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net