JUnit Testing: Getting Started and Getting the Most Out of It

Posted by Chris Tozzi in JUnit

If you're a Java developer, you probably know and love JUnit. It's the go-to tool of choice for unit testing (and, as we will see below, other types of testing as well) for Java apps.

In fact, JUnit is so popular that it's the most commonly included external library on Java projects on GitHub, according to a 2013 analysis. No other Java testing framework comes close in popularity to JUnit.

But while JUnit is widely used, are all of the projects that deploy it getting the most out of it? Probably not. Here's a look at what you should be doing to use JUnit to maximal effect.

JUnit Basics

First, though, let's go over the basics of JUnit, just in case you haven't used it before.

Installation

JUnit supports any platform on which Java runs, and it's pretty simple to install. Simply grab the junit.jar and hamcrest-core.jar files from GitHub and place them in your test class path.

Next, add a dependency like the following to junit:junit in the scope test:

<dependency> 
  <groupId>junit</groupId>
  <artifactId>junit</artifactId> 
  <version>4.12</version> 
  <scope>test</scope>
</dependency>

Basic Usage

With JUnit installed, you can begin writing tests. This process has three main steps.

First, create a class, which should look something like this:

package junitfaq;

import org.junit.*;
import static org.junit.Assert.*;

import java.util.*;

public class SimpleTest {

Second, write a test method, such as:

@Test
   public void testEmptyCollection() {
      Collection collection = new ArrayList();
      assertTrue(collection.isEmpty());
}

… and third, run the test! You can do that from the console with:

java org.junit.runner.JUnitCore junitfaq.SimpleTest

There’s lots more you can do, of course. For all the nitty-gritty details of writing JUnit tests, check out the API documentation.

Getting the Most out of JUnit

Now you know the basics of JUnit. But if you want to run it in production, there are some pointers to keep in mind in order to maximize testing performance and flexibility. Here are the two big ones:

  • Use parallel testing, which speeds up your testing enormously. Unfortunately, JUnit doesn’t have a parallel testing option built-in. However, there’s a Sauce Labs article dedicated to JUnit parallel testing, which explains how to do it using the Sauce OnDemand plugin.
  • Despite the tool’s name, JUnit’s functionality is not strictly limited to unit testing. You can also do integration and acceptance tests using JUnit, as explained here.

If you use Eclipse for your Java development, you may also want to check out Denis Golovin's tips for making JUnit tests run faster in Eclipse. Most of his ideas involve tweaks to the Eclipse environment rather than JUnit-specific changes, but anything that makes Eclipse faster is a win in my book. And of course, don’t forget Sauce Labs' guide to testing best practices. They're also not JUnit-specific, but they apply to JUnit testing, and they're good to know whether you use JUnit or not.

Chris Tozzi has worked as a journalist and Linux systems administrator. He has particular interests in open source, Agile infrastructure and networking. He is Senior Editor of content and a DevOps Analyst at Fixate IO.

Discuss: JUnit Testing: Getting Started and Getting the Most Out of It
0 Comments

Free Trial

Get access to a free 14-day trial version, or contact Sales for more information.