Pursuing Code quality: unit Testing of AJAX applications

Source: Internet
Author: User

Use GWT to test asynchronous applications more easily

Introduction: You may have a great deal of fun writing Ajax applications, but performing unit tests on them is a pain in the neck. In this article, Andrew Glover to address the weaknesses of Ajax, one of which is the inherent challenge of performing unit testing for asynchronous WEB applications. Luckily, he found it easier than expected to solve this particular code quality problem with the help of Google Web Toolkit.

Ajax is undoubtedly one of the most fashionable words in the WEB development community in the near future-the proliferation of AJAX-related tools, frameworks, books and Web sites is the best proof of the technology's popularity. In addition, AJAX applications are pretty neat, aren't they? However, as anyone who has developed an AJAX application confirms, it's really inconvenient to perform tests on Ajax. In fact, the advent of Ajax has fundamentally invalidated many test frameworks and tools because they are not designed for asynchronous WEB application Testing!

Interestingly, a developer with an Ajax-enabled framework noticed this limitation and made some very novel designs for it: built-in testability. In addition, because the framework simplifies the creation of AJAX applications using Java™ code rather than JavaScript, it starts very high and leverages the undoubted standard test framework on the Java platform: JUnit.

The framework I'm talking about is of course the very popular Google Web Toolkit, GWT. In this article, I'll show you how GWT actually leverages Java compatibility to make every part of an AJAX application test like the corresponding synchronization application.

JUnit and Gwttestcase

Because GWT-related AJAX applications are written in Java code, they are ideal for developers to test with JUnit. In fact, the GWT development team has also created a helper class gwttestcase that extends from JUnit's 3.8.1 TestCase. The base class adds functionality to test your GWT code and handle some of the underlying implementations to start and run the GWT component.

The caveat: Gwttestcase is not used to test UI-related code-it is designed to facilitate testing of asynchronous problems that are triggered by UI interaction. The misunderstanding of Gwttestcase uses has frustrated many developers who have just come in contact with GWT because they expect to be able to easily simulate the user interface, but it turns out to be futile.

The Ajax component has two basic components: experience and functionality, which are designed to be asynchronous. Figure 1 illustrates a simple Ajax component that simulates a Web form. Because the component supports Ajax, the form's submission is performed asynchronously (that is, there is no need to reload the page associated with the traditional form submission).

Figure 1. A simple Web form that supports Ajax

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.