Getting started with unit testing in java using JUnit

You have just finished working on a software for a client that is to be delivered in a few days and everything is working as they should, or so you thought. Fast forward to a week later, you start receiving complaints from your client about your software which has started to fail under certain circumstances. Anxiety starts to kick in and you are already regretting that decision you made to skip writing tests as an excuse to speed up the development process.

Well this is just an imaginary scenario to illustrate that things do not always go as planned. In the real world, assuring software quality through tests is an integral part of the software development process. There are a range of tests that can be carried out at different levels but this article will focus solely on unit testing in java using the JUnit test framework. But before we start, lets look at what we are dealing with here.

What is Unit Testing

Unit Testing, as the name implies, are tests written for testing small snippets of code, like function/procedure blocks or classes in isolation. They have an important role in the Agile development proccess and are very low-level as they deal with components close to the source of an application.

Have you ever come across a small piece of code that looked like this:

This snippet of code was taken from codewars.com. As you can see, it includes a class called Tests within which is written a small function called Test1. Notice the method assertEquals inside the function. This method is part of the JUnit API, which will be covered below. What this method is doing is simply testing that TRUE is returned when the HighAndLow function from the Kata class is presented with a string of numbers, having numbers 42 as the highest number and -9 as the lowest number. Here's the code for the function HighAndLow:

Unit Testing using JUnit

JUnit is a popular open source framework for writing unit tests in java. It provides a set of annotations which we can use to specify how we want our tests to be carried out. You can find a list of them in JUnit's official documentation(JUnit 5).

Let's get started by writing a small Calories Calculator program which will output the amount of calories a vegetable contains to a text file. To keep things well structured we will define our main class and our tests in two separate packages.

We are going to test the four methods of our Vegetable class, which are:getCalories()getPercentProtein()getPercentFat()getPercentCarbo()

Now that out main program has been written it's time to write the unit tests to see if our methods/classes are working as they should. We start by importing the JUnit framework into our VegetableTest.java class like this:

After our class has been declared and initialized we are going to write a test for each method. To declare a basic test case, we use the @Test annotation. Since we want to test that the Vegetable class methods are returning expected values, we use the assertEquals method which was explained above. It takes two parameters; the second parameter is the method we are testing and the first parameter is the value we are expecting from that method.

Now that we have written our tests we need to run them. Different IDEs have different ways of running these tests. You can read about how to run them here

Here are the results of the tests:

As you can see they are all successful. Now let's see what happens if we change the expected value for vegetableFatTest from 0.198 to 0.197

Eventually the vegetableFatTest fails and you can see why in the error logs.

This is a very simple example. There are many more concepts involved in writing unit tests, like object mocking for instance. Besides, in this example we wrote tests for every methods of our class. A Real-world software may have a few million lines of code. You can't write tests for testing every functionality. This is why it's important to identify what you want to test first, focus on important pieces of code rather than trival ones.