76
GWT Testing
JUnit provides time-tested framework for testing GWT application. It consists of many tools which can directly create the test cases according to user needs.
Now, on the basis of web-project STOCK EXCHANGE(created in previous chapter) we perform JUnit testing.
- Like all GWT JUnit test cases, the StockExchangeTest class extends the GWT Test Case class in the com.google.gwt.junit.client package. You can create additional test cases by extending this class.
- The StockExchangeTest class has an abstract method (getModuleName) that must return the name of the GWT module. For StockExchange, that is com.google.gwt.sample.stockexchange.StockExchange.
- The StockExchangeTest class is generated with one sample test case tautological test, testSimple. This testSimple method uses one of the many assert functions that it inherits from the JUnit Assert class.
- The assertTrue(boolean) function asserts that the boolean argument passed in evaluates to true. If not, the testSimple test will fail when run in JUnit.
StockExchangeTest.java
Running unit tests
You can run JUnit tests four ways:
- from the command line, using the scripts generated by junitCreator
- in Eclipse, using the Google Plugin for Eclipse
- in Eclipse, using the Eclipse launch configuration files generated by webAppCreator
- in manual test mode
We are using Eclipse and Google plug-in:
The Google Plugin for Eclipse makes it easy to run tests in Eclipse.
- Run the JUnit test in development mode.
- From Package Explorer, right click on the test case you want to run, select Run As > GWT Junit Test
- The simpleTest executes without error.
- Run the JUnit test in production mode.
- From Package Explorer, right click on the test case you want to run, select Run As > GWT Junit Test (production mode)
- The simpleTest executes without error.
Writing unit test
- Write a JUnit test to verify that the constructor of the StockPrice class is correctly setting the new object’s instance fields.
- To the StockExchangeTest class, add the testStockPriceCtor method as shown below.
Rerun StockExchangeTest in development mode.
Both tests should pass:
Next TopicGWT JSON