«    »

Java Unit Testing Tutorial

This tutorial explains how to set up and write automated unit tests in Java using JUnit 4, a widely used java unit testing framework, Eclipse 3, an open source integrated development environment (IDE), and Java SE 5 or later. This tutorial is intended to be an introduction to unit testing aimed at developers who have limited or no experience with it. A working knowledge of Java (including Java language features new to version 5) and Eclipse is assumed. The vision for this tutorial is to enable you to adopt unit testing as part of your Java development activities by explaining how to setup, write, and use unit tests.

What are automated unit tests? They are a type of testing performed by developers to ensure that the code they write works as intended. They are automated: tests are expressed as test code that automatically invokes the code to be tested and verifies that the results of execution match the developer's expectations. The scope of testing is individual units of functionality within the code base, such as a single class or method.

This tutorial is oriented towards beginners and therefore is prescriptive. It provides a single step-by-step method for setting up and writing unit tests, usually without explaining the reasons behind why a particular method was used or what some alternative options may be.

This tutorial assumes you already have a Java project with some code (maybe only a single class) set up in the Eclipse IDE. As you go through the tutorial, you should apply the instructions to your project. The tutorial uses a sample project called UnitTestingTutorial to provide concrete examples when appropriate. Use the link below to download this project.

Download the Unit Testing Tutorial Example Project v1.0

This tutorial is divided into a number of sections, each of which is presented as a separate page:

I plan to add additional sections to this tutorial over time. If there is particular content you are interested in let me know in the comments below.

Next: Section 1: Initial Setup

If you find this article helpful, please make a donation.

«    »