On the Web: Testing Javascript with Apache Maven

March 18, 2010 By Tim OBrien

3 minute read time

A few years ago Javascript was still something of a curiosity for most web developers. Fast forward to 2010, and most web applications are using liberal amounts of Javascript to take advantage of AJAX interactions and REST services. Once a curiosity, Javascript is now a primary artifact in a web project.

Javascript deserves the same build support as other source code. It needs to be tested as a part of the build and integrated into the overall build lifecycle. Stéphane Épardaud writes a great how-to about using Rhino, the Maven Jetty plugin, and a Maven plugin called Testlol to run AJAX-enabled JavaScript unit tests in Maven. Check out his blog post, it is a testament to the flexibility of Maven that it can be adapted to support such a wide range of languages and technologies: from Flex to Java to Javascript, all within the same unified project structure and lifecycle.

Here's an excerpt from Épardaud's original post:

While adding a new feature to RESTEasy — the generation of JavaScript skeleton code to invoke RESTful web services implemented in JAX-RS — I candidly proceeded to write unit tests for that part. Unit testing this new feature meant making sure that not only was the JavaScript code produced by the web services (in our case, produced by a Servlet, and thus running in a Servlet container), I also wanted to make sure that the JavaScript code was valid, contained all the functions required and that the functions worked. Since the generated functions use AJAX to call the web services, that leaves us with the following constraints on what we need in order to test this:

  • A test framework integrated into the project's build tool, Maven
  • JAX-RS web services whose JavaScript skeleton code to produce, and to invoke
  • A Servlet container to run those JAX-RS web services, as well as my JavaScript generator Servlet
  • A JavaScript interpreter that I can use in Java (since this is the RESTEasy implementation language)
  • A JavaScript interpreter that also can do AJAX
  • A JavaScript interpreter that supports JavaScript 1.8 (since I use a feature introduced in that version: native JSON)

Read Épardaud's complete post, it takes you through the process of installing the appropriate JARs and configuring your POM to test Javascript with Maven.

Tags: Nexus Repo Reel, Apache Maven, Everything Open Source, Maven Jetty plugin, Javascript

Written by Tim OBrien

Tim is a Software Architect with experience in all aspects of software development from project inception to developing scaleable production architectures for large-scale systems during critical, high-risk events such as Black Friday. He has helped many organizations ranging from small startups to Fortune 100 companies take a more strategic approach to adopting and evaluating technology and managing the risks associated with change.