Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

JavaWorld Daily Brew

Thinking Inside a Bigger Box

Welcome to Johannes Brodwall's blog. I use this space to work on articles mostly about software development, with a focus on Java, SOA, and Agile software development. Many of the articles you will find here are not much more than drafts, and I certainly appreciate input on how to make them better.

If you wonder about the title of this blog, Thinking Outside the Box may answer your questions.

I work as the lead software architect of BBS, the company that handles interbank services in Norway. In my copious free time, I develop software and consult companies in development practices and architecture. For more about the services I can offer, please see my resume.


A canonical web test

 

In order to smoke test web applications, I like to run-to-end smoke tests that start the web server and drives a web browser to interact with the application. Here is how this may look:

public class BookingWebTest {
 
    private DataSource dataSource;
    private Server server;
 
    @Before
    public void createServer() throws Exception {
        dataSource = DataSources.getTestDataSource();
        new EnvEntry("jdbc/applicationDs", dataSource);
 
        server = new Server(0);
        server.setHandler(new WebAppContext("src/main/webapp", "/test"));
        server.start();
    }
 
    private final WebDriver browser = new HtmlUnitDriver();
 
    @Test
    public void shouldShowCreatedBookings() throws Exception {
        PersonDao personDao = new PersonDao(dataSource);
 
        Person person = new Person();
        person.setName("Person " + Math.random());
        personDao.save(person);
 
        browser.get(server.getURI() + "/test/people/");
        assertThat(browser.findElement(By.id("people")).getText())
            .contains(person.getName());
    }
}

This test is in the actual war module of the project. This is what it does:

  1. Configures the application to run towards a test database
  2. Starts up the web server Jetty on an arbitrary port (port = 0) and deploys the current web application into Jetty
  3. Fires up HtmlUnit which is a simulated web browser
  4. Inserts an object into the database
  5. Navigates to the appropriate location in the application
  6. Verifies that inserted object is present on the appropriate page

This test requires org.eclipse.jetty:jetty-server, org.eclipse.jetty:jetty-webapp and org.seleniumhq.selenium:selenium-htmlunit-driver to be present in the classpath. When I use this technique, I often employ com.h2database:h2 as my database. H2 can run in-memory and so the database is fresh and empty for each test run. The test does not require you to install an application server, use some inane (sorry) Maven plugin or create any weird XML configuration. It doesn’t require that your application runs on Jetty in production or test environment – it work equally fine for Web applications that are deployed to Tomcat, JBoss or any other application server.

Please!

If you are developing a web application for any application server and you are using Maven, this trick has the potential to increase your productivity insanely. Stop what you’re doing and try it out:

  1. Add Jetty and HtmlUnit to your pom.xml
  2. Create a unit test that starts Jetty and navigates to the front page. Verify that the title is what you expect (assertEqual("My Web Application", browser.getTitle()))
  3. Try and run the test

Feel free to contact me if you run into any trouble.