Unit Testing
Unit testing is a testing method where small units of source code (i.e. classes, methods) are verified for their expected behavior. Once a unit test is implemented for a specific test case, the same test can be reused over and over again. As a result, unit testing is essential for efficient software development.
For Java, JUnit is the golden standard testing framework. As such, we will use JUnit (v4.13) to implement our unit tests.
This section will examine how to install JUnit (v4.13) into the current project and write simple unit tests using JUnit.
Table of Contents
Installing JUnit Into The Current Project
This instruction set contains steps for installing the JUnit (v4.13) dependency needed to write unit tests in IntelliJ IDEA.
Navigate to [File] > [Project Structure…]. A new “Project Structure Window” should appear.
Note: This option only installs JUnit to the current project. To install JUnit for all future projects, navigate to [File] > [Other Settings] > [Structure for New Projects…].Click on [Modules] > [Dependencies] to display dependencies installed in the current project.
Click on [+] > [Library…] > [From Maven…] at the bottom of the “Project Structure Window” to add a new dependency to the current project.
In the search box, type
junit:junit:4.13
, then click [OK].
A new “Configure Library Window” should pop up. Enter a ‘Name’ for the library (e.g.JUnit (v4.13)
) and click [OK] until you exit to the “Main Window”.
Download MyDivision.java and MyDivisionTest.java and add the two files to your project folder. Select MyDivisionTest.java from the left pane and check for any errors. If the installation was successful, there should be no errors with the file.
Caution: Make sure JUnit is correctly installed in your current project and that MyDivision.java and MyDivisionTest.java are added to your project folder.
Writing Unit Tests
Now we will analyze a simple unit test to help us understand the code structure of unit tests. Afterwards, we will modify the code to test another test case.
MyDivision.java is a basic class with a single method defined, called ‘divide’. The method receives two parameters and returns the quotient of the given number.
MyDivisionTest.java is a test suite for MyDivision.java. Please refer to the diagram below.
- Import statements - we need to import at least ‘@Test’ annotations and ‘assert’ statements to run any test(
import org.junit.Test
andimport static org.junit.Assert.*;
, respectively). - ‘@Test’ annotation - these annotations signal to the compiler that the method defined below is a test case for unit testing.
- Assert statements - tests whether given values fall under the provided condition (e.g. ‘assertEquals’ tests whether the given values are equal).
If we take a look inside the ‘testDivideReturnsCorrectQuotient’ method, it checks whether the result of 4 divided by 2 is equivalent to 2. Since 4 divided by 2 does indeed equal 2, the test should pass without failure when we run it. Go ahead and try running the test by right-clicking on [MyDivisionTest.java] and clicking on [Run ‘MyDivisionTest’].
The following result should display:
We can add a new test case to the current test suite. Let’s copy the ‘testDivideReturnsCorrectQuotient’ method (highlighted in the image below) and rename the method to ‘testDivideByZero’. MyDivisionTest.java should look like below.
@Test
public void testDivideByZero() {
MyDivision myDivisionClass = new MyDivision();
final int expected = 0;
final int result = myDivisionClass.divide(4, 0);
assertEquals(expected, result);
}
If we change the second parameter of ‘myDivisionClass.divide’ to 0
, then what will our outcome be? Try running the test and see for yourself.
The test results should be as following:
We can see that the test we just defined failed due to an ‘ArithmeticException’. In this case, a division by zero is undefined. Therefore, the program could not handle the situation, threw an exception, and crashed. Failed tests identify the problem in our code. In this case, an exception is thrown when the second parameter is 0
.
To ensure the test passes, we should modify our ‘divide’ method inside MyDivision.java for special cases when the divisor is 0
. Let’s assume all division by 0
should return 0
and modify our method to the following:
public int divide(int dividend, int divisor) {
if (divisor == 0) {
return 0;
}
return dividend / divisor;
}
If we run the test again, both tests should pass without failure. Through failed tests, we were able to correct our code. Now you should have a better understanding of how to create unit tests.