Table of contents
1.
Introduction
2.
GWT Testing
3.
JUnit test creation
3.1.
StockExchangeTest.java
4.
Running Unit Tests
5.
Writing Unit Test
6.
Frequently asked questions
6.1.
What is GWT testing?
6.2.
What is the GWT's purpose?
6.3.
What benefits does GWT offer?
6.4.
Why is GWT used?
6.5.
What Does The Gwt .cache.html File Mean?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

GWT Testing

Author Ankit Kumar
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this article, we will discuss GWT Testing. But before we further learn about GWT Testing, let's have a bit of knowledge about GWT for better understanding.

GWT (Google Web Toolkit) is a development toolkit used to create and improve complicated browser-based apps. It aims to make it simple to create high-performance web apps productively without the developer having to be an expert in JavaScript, XMLHttpRequest, or browser quirks. Thousands of developers worldwide use it, which is open source and free.

Introduction Image

In the next section, we will understand the concept of GWT Testing.

GWT Testing

GWT's testability is one of its key features; as a result, we can quickly test our applications using various tried-and-true testing methods. The three types of testing components that make up GWT apps are as follows:

  • Standard JUnit TestCases 
  • GWTTestCases (subclasses of the JUnit TestCase) 
  • Selenium Testing
gwt testing

Since GWT application code runs as Java rather than JavaScript, testing a GWT application could look a little complicated. However, it is pretty easy to use these testing components to test our application correctly. Moreover, we can use powerful design principles in our code to make our test cases short, efficient, and maintainable.

JUnit test creation

JUnit offers a tried-and-true testing framework for GWT applications. It has multiple tools that let users directly generate test cases that fit their needs.

When you selected the -junit option, WebAppCreator created all the files required to start creating JUnit tests. It also creates launch configuration files for Eclipse, a first test class, and ant targets for command-line testing. 

JUnit test creation

Now, we are performing JUnit testing based on the web project StockExchange (built in the previous chapter).

  • The StockExchangeTest class extends the GWT Test Case class in the com.google.gwt.junit.client package, just like all other GWT JUnit test cases do. By extending this class, you can build more test cases.
     
  • The getModuleName abstract function of the StockExchangeTest class is required to return the name of the GWT module. That is com.google.gwt.sample.stockexchange.StockExchange for StockExchange.
     
  • One example test case, testSimple, a tautological test, is generated for the StockExchangeTest class. One of the various assert functions that the testSimple method inherits from the JUnit Assert class is used.
     
  • The assertTrue(boolean) function claims that the value of the boolean argument is true. If not, running the testSimple test in JUnit will fail.

StockExchangeTest.java

package com.google.gwt.sample.stockexchange.client;  
    import com.google.gwt.junit.client.GWTTestCase;  
    
    /**  GWTTestCase must be extended in GWT JUnit tests. */   
 
public class StockExchangeTest extends GWTTestCase {                         
 
        /**  Must name a valid module that uses this class as a source. */  
   
  public String getModuleName() {      
  return "com.google.gwt.sample.stockexchange.StockExchange";  
  }    
      /**  As many tests as you want to add.  */ 


  public void testSimple() {                                                
assertTrue(true);  
  }  
    }  

Running Unit Tests

In this section, we will learn how to run a Junit Test case. You can create your Unit Test case, But before you begin developing your unit tests for StockWatcher

make sure the elements of the test environment are in place. You can do this by launching StockWatcherTest.java, which will run the testSimple starting test.

Running and writing unit tests

There are four ways to execute JUnit tests:

  • From the command line – using the junitCreator-generated scripts.
  • In Eclipse – using the Eclipse Google Plugin.
  • In Eclipse – utilising the web app creator-produced launch configuration files for Eclipse.
  • Manual test mode


We are using Eclipse and Google plug-in to Run Unit Test Case. Thanks to the Google Plugin for Eclipse, it is simple to run tests in Eclipse.

  • Open development mode and run the JUnit test.
  • Right-click the test case you want to run in Package Explorer and choose Run As > GWT Junit Test.
  • SimpleTest runs without throwing errors.
  • Now, Run the JUnit test in the production mode.
  • Right-click the test case you want to run in Package Explorer and choose Run As > GWT Junit Test (production mode).
  • SimpleTest runs without throwing errors.

In the next section, we will learn to write a unit test.

Writing Unit Test

In a real testing scenario, you would want to confirm the functioning of as much of StockWatcher as possible. To the StockWatcherTest class, a variety of unit tests might be added. Each test would be written as a public method.

You could organise test cases if you had a bunch of them by classifying them into various test groups. But in this article,  you'll only write and execute one test to learn how to set up JUnit tests in GWT.

  • To make sure that the instance fields of the new object are being set appropriately by the StockPrice class' function Object() { [native code] }, create a JUnit test.
  • As shown below, add the testStockPriceCtor method to the StockExchangeTest class.
/** 
     * Make that the StockPrice class' instance fields are correctly set.
 */  
public void testStockPriceCtor() {  
  String symbol = "XYZ";  
  double price = 70.0;  
  double change = 2.0;  
  double changePercent = 100.0 * change / price;  
  StockPrice sp = new StockPrice(symbol, price, change);  
  assertNotNull(sp);  
  assertEquals(symbol, sp.getSymbol());  
  assertEquals(price, sp.getPrice(), 0.001);  
  assertEquals(change, sp.getChange(), 0.001);  
  assertEquals(changePercent, sp.getChangePercent(), 0.001);  
}  

 

  • Now in development mode, run StockWatcherTest once more.
  • Both tests need to succeed.
[junit] Running com.google.gwt.sample.stockwatcher.client.StockWatcherTest
[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 16.601 sec


So this was all about the GWT Testing, hope! You will have fun creating your test case for testing and will let me know in the comments below. 

Let us now see some of the frequently asked questions related to this.

Frequently asked questions

What is GWT testing?

GWT offers several testing tools and associated supporting classes that enable testing against Java bytecode in hosted mode or against JavaScript in web mode. These ideas also hold while utilising the GWT shell for development.

What is the GWT's purpose?

A GWT development toolkit is used to create and improve complex browser-based apps. Its objective is to make it possible to construct high-performance web apps productively without the developer having to be an expert in JavaScript, XMLHttpRequest, or browser quirks.

What benefits does GWT offer?

AJAX app creation is made simple with the Google Web Toolkit (GWT), an open-source Java software development platform. The Java development tools can be used to develop and debug GWT AJAX applications.

Why is GWT used?

The GWT compiler optimises the resulting code, removes dead code, and even obfuscates the JavaScript.

What Does The Gwt .cache.html File Mean?

It includes a GWT application's actual programme.

We have come so far, and it's time to close the discussion.

Let us now move to the conclusion part of this article.

Conclusion

In this article, we've extensively discussed GWT Testing with the help of Junit test creation, used Eclipse and Google plug-in to Run Unit Test Case, wrote and executed one test to learn how to set up JUnit tests in GWT, and at last, some frequently asked questions related to the topic.

Ninjas do not stop here!

You can also explore more articles on GWT GridGWT StackLayoutPanelGWT RootLayoutPanelGWT MVP, and GWT Charts or access the Coding Ninjas Studio for other courses.

Happy Coding!

Thanking Image
Live masterclass