RSS Feed Subscribe to RSS Feed


Running an individual unit test with maven

Individual tests can be run from maven. This must be done from within the folder that contains the source (as in the folder that contains src/test/java), which will likely be either be the root of your maven project, or in one of your modules.

For surefire (unit tests), this will be something like:

mvn -Dtest=com.your.package.YourTest test

For failsafe (integration tests), it will be something like:

mvn -Dit.test=com.your.package.YourIT verify

This is covered in the maven surefire plugin docs and maven failesafe plugin docs respectively, but those examples don’t mention packages names, which are required.

Tags: , , , , , , ,

Running tests in IntelliJ for a multi module maven project

This post shows how to run all the tests in IntelliJ for a multi module maven project.


Tags: , , ,

Unit Testing – the Hard Parts @ NYC Code Camp

Thanks to everyone who came to my “Unit Testing – the Hard Parts” presentation @ New York Code Camp. I really enjoyed it! Great crowd, lots of follow up questions. And cool Microsoft office space in Times Square. Thank you to all who attended. My slides are here:

Tags: , , , , , , , , ,

Test Doubles: mocks, dummies and stubs

Most classes have collaborators. When unit testing, you usually want to avoid using real implementations of those collaborators to avoid test brittleness and binding/coupling, and instead use Test Doubles: Mocks, Stubs and Doubles.

This article references two existing articles on the subject: Mocks Aren’t Stubs, by Martin Fowler and The Little Mocker, by “Uncle” Bob Martin. I recommend them both.



Tags: , , , , , , , , , , ,

Builder Pattern: Good for code, great for tests

I’ve found the builder design pattern occasionally useful in code, but frequently useful in tests. This article is a quick summary of the pattern in general, followed by look at a working example of using it in tests. See the code in github.

Tags: , , ,

Software Quality via Unit Testing

The following post is based on a talk I gave at Desert Code Camp 2013. See also the associated slide deck.

Software quality is critical to consistently and continually delivering new features to our users. This articles covers the importance of software quality and how to deliver it via unit testing, Test Driven Development and clean code in general.
Read more

Tags: , , , , , , , , , ,

Spring MVC Hello World with Continuous Deployment

Oh dear, yet another ‘Hello World!’. But although the functionality is trivial, this little SpringMVC project is complete enough for me to use as a template to bootstrap more complex projects. It consists of:

  • HTML/JSP client
  • SpringMVC server using a Controller/Service/DAO design
  • Maven for build and dependency management

This is an updated version of an older project I created, with the following enhancements:

  • Added a full suite of automated tests (unit, integration and browser based)
  • Added placeholders for JavaScript and images (both can be a little tricky to put in the correct place with SpringMVC)
  • Incorporated into a continuous deployment environment

More details below, but you can find the full source code on this GitHub repository and the deployed app at
Read more

Tags: , , , , , , , , , , ,

Hamcrest Matcher

As a follow up to the Hamcrest post I made yesterday, I wanted to post an example of my own Hamcrest matcher implementation. This matcher tests if a string contains a specified number of substrings.
An example usage could be:

    String sql = "select a,b,c from tableA";
    assertThat(sql, hasNumberOfSubstrings(",", 2));

See the source code below. I have been reading up on OSS licenses recently and decided to release this using the same license as Hamcrest – the new BSD license.

I have also attached a jar which includes the associated unit tests, although you will need the hamcrest-unit-test project to compile, which can be downloaded as part of the hamcrest all-in-one jar.
Read more

Tags: , , ,


Hamcrest is a framework for writing matcher objects. Matchers have a variety of uses, but are particularly useful when writing unit tests. Instead of using JUnit’s assertEquals methods, we use Hamcrest’s assertThat construct with one (or more) of the many Matchers available. For example



    assertThat(a, equalTo(b));

A small change in this example, but Hamcrest’s benefits are many, enabling you to write much more flexible tests that are easier to read and have more meaningful failure messages.
Read more

Tags: , ,

Selenium talk at SF JUG

I attended another great San Francisco JUG meeting tonight, this time on How to use Selenium with Maven/Ant to automate testing of web apps.

The talk was given by Chris Bedford, from Build Lackey Labs – “Automating the Monkey Work Out and the Quality In!”. Overall, I thought this was a great talk by Chris. He clearly has a huge amount of experience creating automated tests and integrating them with build tools and he gave a well structured, interesting, well delivered presentation. I have posted a copy of Chris’s slides and I think the video will be posted on the SF JUG site at some point, but I have also posted my notes from the presentation below…
Read more

Tags: , , , , ,