A Simple Unit Test

Jakob Jenkov
Last update: 2014-05-24

In this text I will show you a simple unit test implemented using JUnit 4.8.1.

First I will show you the class I want to test:

public class MyUnit {

    public String concatenate(String one, String two){
        return one + two;
    }
}

I have kept the class very simple to make it eaiser to understand what is going on.

The Unit Test

To test this class I need a unit test that test each of its public methods. The class only has one public method, concatenate(), so all I need to test is this method.

Unit tests are implemented as classes with test methods. Each test method usually tests a single method of the target class. Sometimes, a test method can test more than one method in the target class, and sometimes, if the method to test is big, you split the test into multiple test methods.

Here is the JUnit unit test that test that the concatenate() method:

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

public class MyUnitTest {

    @Test
    public void testConcatenate() {
        MyUnit myUnit = new MyUnit();

        String result = myUnit.concatenate("one", "two");

        assertEquals("onetwo", result);

    }
}

The unit test class is an ordinary class, with one method, testConcatenate(). Notice how this method is annotated with the JUnit annotation @Test. This is done to signal to the unit test runner, that this is method represents a unit test, that should be executed. Methods that are not annotated with @Test are not executed by the test runner.

Inside the testConcatenate() method an instance of MyUnit is created. Then it's concatenate() method is called with two string values.

Finally, the assertEquals() method is called. It is this method that does the actual testing. In this method we compare the output of the called method (concatenate()) with the expected output. In other words, we compare "onetwo" (expected output) with the value returned by the concatenate() method, which is kept in the variable result.

If the two values are equal, nothing happens. The assertEquals() method returns normally. If the two values are not equal, an exception is thrown, and the test method stops executing here.

The assertEquals() method is a statically imported method, which normally resides in the org.junit.Assert class. Notice the static import of this class at the top of MyUnitTest. Using the static import of the method is shorter than writing Assert.assertEquals().

You can have as many test methods in a unit test class as you want. The test runners will find them all, and execute each of them. I just sufficed with one test method in this example, to keep it clear.

That's it. This is how simple a unit test can be with JUnit 4.8.2 .

Jakob Jenkov

Featured Videos

Java Generics

Java ForkJoinPool

P2P Networks Introduction



















Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next