Skip to content
 

Unit testing with JUnit

Until fairly recently unit testing is an engineering practice I’ve had little exposure to. It’s perfectly possible to get by developing without unit tests, but they do help with the design, verification and maintenance of code.

A unit test is designed to test a small functional unit of code for a particular scenario. Generally multiple tests are written to verify different paths through the same method/function. Personally I think unit tests are particularly valuable in a continuous integration environment (e.g. Jenkins) where the tests run prior to regular deployment and developers are alerted of any failures.

There are many testing frameworks but for basic unit tests JUnit is pretty much the de-facto standard. The Eclipse IDE comes with a plugin already configured to allow unit tests within a project to be run by simply right-clicking on the project and running the code as a JUnit test. However JUnit tests can also be run stand alone through the command line, an Apache Ant task, or a Apache Maven build. Maven will automatically run unit tests placed in /src/test/java during a build by default.

So what does a simple unit test look like?

import static org.junit.Assert.assertTrue;

import org.junit.Before;
import org.junit.Test;

public class HelloTest {

  private String productName;

  public class SayHello {
    private final String userName;
    private final String productName;
    public SayHello(String userName, String productName) {
      this.userName = userName;
      this.productName = productName;
    }

    public String speak() {
      return "Hello " + userName + " welcome to " + productName;
    }
  }

  @Before
  private void setup() {
    name = getProperty("productName");
  }

  @Test
  public void speakReturnsString_WithCorrectProductAndUsername() {
    SayHello sayHello = new SayHello("Dave", productName);
    assertTrue(sayHello.speak().contains("Dave"));
    assertTrue(sayHello.speak().contains(productName));
  }
}

The annotation @Before defines common code to run prior to the execution of each test method defined by @Test. [The getProperty method has been omitted]

This is a simple test which verifies that the implementation of the construction of a SayHello object followed by execution of the speak method results in a string being returned that contains the userName and productName which were passed in the constructor. This illustrates the difference between desired test behavior and the actual implementation. Here we are allowing the flexibility for the speak() method be modified as long as it returns a string containing both the username and the product name passed in.

Though this is a basic example hopefully this gives an idea of how unit tests are usually used to define desired behaviour (what the code should do) not necessarily implementation (how it does it). Writing tests allows you to clarify the behavior of your code which makes it a useful tool in code design as well as validation.

Leave a Reply