Home » Software Development » Single Statement Unit Tests

About Yegor Bugayenko

Yegor Bugayenko
Yegor Bugayenko is an Oracle certified Java architect, co-founder and CTO of Teamed.io, lead architect of Rultor.com and Jcabi.com, and a big fan of test automation.

Single Statement Unit Tests

Many articles and books have already been written about unit testing patterns and anti-patterns. I want to add one more recommendation which, I believe, can help us make our tests, and our production code, more object-oriented. Here it is: a test method must contain nothing but a single assert.

Bullet (1996) by Julien Temple

Look at this test method from RandomStreamTest from OpenJDK 8, created by Brian Goetz:

public void testIntStream() {
  final long seed = System.currentTimeMillis();
  final Random r1 = new Random(seed);
  final int[] a = new int[SIZE];
  for (int i=0; i < SIZE; i++) {
    a[i] = r1.nextInt();
  final Random r2 = new Random(seed);
  final int[] b = r2.ints().limit(SIZE).toArray();
  assertEquals(a, b);

There are two parts in this method: the algorithm and the assertion. The algorithm prepares two arrays of integers and the assertion compares them and throws AssertionError if they are not equal.

I’m saying that the first part, the algorithm, is the one we should try to avoid. The only thing we must have is the assertion. Here is how I would re-design this test method:

public void testIntStream() {
  final long seed = System.currentTimeMillis();
    new ArrayFromRandom(
      new Random(seed)
    new Random(seed).ints().limit(SIZE).toArray()
private static class ArrayFromRandom {
  private final Random random;
  ArrayFromRandom(Random r) {
    this.random = r;
  int[] toArray(int s) {
    final int[] a = new int[s];
    for (int i=0; i < s; i++) {
      a[i] = this.random.nextInt();
    return a;

If Java had “monikers” this code would look even more elegant:

public void testIntStream() {
    new ArrayFromRandom(
      new Random(System.currentTimeMillis() as seed)
    new Random(seed).ints().limit(SIZE).toArray()

As you can see, there is only one “statement” in this method: assertEquals().

Hamcrest with its assertThat() and its collection of basic matchers is a perfect instrument to make our single-statement test methods even more cohesive and readable.

There are a number of practical benefits of this principle, if we agree to follow it:

  • Reusability. The classes we will have to create for test assertions will be reusable in other test methods and test cases. Just as, in the example above, ArrayFromRandom could be used somewhere else. Similarly, Hamcrest matchers may and will constitute a library of reusable test components.
  • Brevity. Since it will be rather difficult to create a long test method when it only has a single assert, you and your fellow programmers will inevitably write shorter and more readable code.
  • Readability. With a single assert it will always be obvious what the intent of the test method is. It will start with the intent declaration while all other lower level details will be indented.
  • Immutability. It will be almost impossible to have setters in the production code if test methods have no place for algorithmic code. You inevitably will create immutable objects to make them testable with a single assert.

The biggest benefit we get when this principle is applied to our tests is that they become declarative and object-oriented, instead of being algorithmic, imperative, and procedural.

Reference: Single Statement Unit Tests from our JCG partner Yegor Bugayenko at the About Programming blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area from our partners over at ZipRecruiter


Leave a Reply

Your email address will not be published. Required fields are marked *


Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns