Measure Code Coverage of HtmlUnit Based Tests with Sonar and JaCoCo

This blog post is the third one in a series about Integration Tests with HtmlUnit. The first post, titled “Automating Assembly and Integration Tests with HtmlUnit”, showed how to write integration tests of web UI applications using HtmlUnit. That post can be read here. The second post, titled “Integrate HtmlUnit Based Tests with Apache Maven and Cargo”, showed how to automate the execution of HtmlUnit tests using Maven and Cargo plug-in. That post can be read here.

Finally, in this post we are going to show how to measure code coverage of HtmlUnit tests using Sonar, the popular Continuous Quality Assurance tool, and JaCoCo, a very interesting code coverage tool based on JVM agents instead of instrumenting bytecodes.

Continue reading “Measure Code Coverage of HtmlUnit Based Tests with Sonar and JaCoCo”


Integrate HtmlUnit Based Tests with Apache Maven and Cargo

This blog post is a continuation of the one titled “Automating Assembly and Integration Tests with HtmlUnit” that can be read here.

In the previous post we learned how to make use of HtmlUnit API to write automated integration tests. In this post we are going to show how to integrate those tests in an Apache Maven project lifecycle while keeping a clear distinction between the unit test suite and the integration test suite.

Continue reading “Integrate HtmlUnit Based Tests with Apache Maven and Cargo”

Automating Assembly and Integration Tests with HtmlUnit

With this blog entry I want to start a mini-series about automating assembly and integration tests in Java. Mini-series because there are many different tools and techniques to choose from, and depending on the software being developed you will choose and combine to get the best results.

This entry will show the basics on how to use HtmlUnit to automate assembly and integration tests of web applications.

What is HtmlUnit? HtmlUnit is a framework that runs on top of the popular JUnit testing framework that simulates a web browser, including JavaScript. Using the HtmlUnit API you can interact with the web pages and get insights of the layout and contents to write assertions. An interesting fact about HtmlUnit is that you can use to automate tests of web applications written in other languages than Java.

HtmlUnit can be downloaded from the tool web site at

With HtmlUnit there is no practical distinction when you intend to write an assembly or an integration test case. If you choose to run with an embedded (or in memory) database and web application server or with a dedicated one will mark the (sometimes controversial) distinction between an assembly or an integration test.

In this entry we are focusing on HtmlUnit setup and common use cases and for a later entry we will focus in assembly vs. integration setup, best practices and how to run them within a continuous build/integration process.

Continue reading “Automating Assembly and Integration Tests with HtmlUnit”

Testing Java Swing components with Fest

entrada antigua del 28/3/2011 – old post from 3/28/2011


While exploring into new ways of writing automated unit test cases in Java, I discovered the Fest framework.

Besides some nice tools for mocking and wrapping calls to reflection, Fest has a convenient API to automate unit tests for Swing components leveraging the AWT Robot, which is too low-level as to be of real use for massive unit testing.

Of course, this is valid for integrated/assembly tests, not real unit tests, because Fest launches a Swing frame or dialog and then reproduces user gestures in the application (go to that field, enter that text, select that on the combo, press this button and so on). I find it useful because, especially in Swing, a thorough unit test suite is not enough to ensure proper user experience and bug-free deliveries. You need to test integrated, all the Swing widgets, event handlers and even business logic.

Results have been very good. Fest has some bugs but even with that I was able to write a good quantity of unit tests in a decent time. I will show some examples in this post.

Continue reading “Testing Java Swing components with Fest”

Using EasyMock to enable response text based testing of Java Servlets

entrada antigua del 23/3/2011 – old post from 3/23/2011


Behind this long title, today I would like to show an easy but powerful technique to unit test Java Servlets.

This method focuses in the fact that most of the Java Servlets are just returning plain text contents, usually HTML or XML.

For a proper unit test on the Java Servlet to run outside a web container, we need to mock the HttpServletRequest and HttpServletResponse objects. Mock, in short, is a term to refer to a double object, that is, an object that exists solely for the purpose of testing the unit at hand, with the same interface that an actual runtime object but with a different behavior, based on expectations.

It is not the purpose of this post to talk about mocks and other unit testing techniques. If you feel curious you can get more on the subject on this excellent paper from Martin Fowler (yes, ‘that’ Fowler guy): Mocks aren’t Stubs, by Martin Fowler.

With that being said, let’s go to the point. What we want to achieve is to test the output of a Java Servlet in different situations, asserting that this output contains certain strings of text. We will need to mock the request and response objects.

Continue reading “Using EasyMock to enable response text based testing of Java Servlets”