Note this blog is obsolete, see for the current blog

If you want to do integration testing using HtmlUnit and Maven the following sets you up for easy testing.

1. Create a Maven web application project
2. Add index.jsp page (or whatever flavor of framework you want to use).
3. Add HtmlUnit dependency
4. Write integration test for the index.jsp page
5. Add the profile to support running of the integration tests separate from the main build cycle

Create a Maven web application project

First create the project. Below is how the POM file looks like that I started with.
Note that there is nothing specific to HtmlUnit yet.


Add index.jsp page

Below the very simple JSP page used for setting up integration testing,
add it to $PROJECT_ROOT/src/main/webapp/

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Testing with HtmlUnit</title>
        <h1>Testing with HtmlUnit + Maven!</h1>

Add HtmlUnit dependency

The next step is to add the HtmlUnit dependency so we can write our integration test.

Add the dependency snipppet below to your POM file.


Write integration test for the index.jsp page

And now it is time to write the actual integration test, see the code below

package com.manorrock.demo.htmlunit;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

public class HtmlUnitIT {

    private String webUrl;

    private WebClient webClient;

    public void setUp() {
        webUrl = System.getProperty("integration.base.url");
        webClient = new WebClient();

    public void tearDown() {

     * Test /index.html
    public void testIndexHtml() throws Exception {
        System.out.println("Connecting to: " + webUrl);
        HtmlPage page = webClient.getPage(webUrl);
        assertTrue(page.getBody().asText().indexOf("Testing with HtmlUnit + Maven!") != -1);

Add the integration test profile

To support running the integration test outside of the main build cycle add the following profile


Note that in order for the integration profile to work you will have to deploy the web application
to the application server of your choice.

To run the integration tests use the following command line: mvn -Pintegration verify

If you did not deploy the application at http://localhost:8080/htmlunit you will need
to set the integration.base.url.

If your application is deployed at, the command line would
be: mvn -Dintegration.base.url= -Pintegration verify

More about integration in the next article!