Article cover






Unit Testing

Unit Testing with Java

Pre-requirements of this reading: basic knowledge of Java, OOP

What is a unit test ?

A unit test is simply a small piece of code written by the engineer that executes a specific functionality and the code which is tested. It targets a small section of code for example, a method or class. The test coverage is important as this is a percentage of code which is tested by the unit tests.

Why do we write unit tests ?

Unit tests are extremely important as it ensures that your code works as intended. It’s also effective as they ensure that your code still works when future functionality is added or fixing a bug. Engineers have added new features into a system, it should do a regression which is testing all of the code that it’s written for on the unit tests to make sure that nothing has been broken with the added new fix. Also, unit tests help testers and developers to understand the code.

How can we write unit tests in Java ?

Various types of unit testing tools available in the market for different languages. For example, NUnit, JUnit, PHPunit, Parasoft Jtest, EMMA. As a Java programmer we will examine JUnit framework. In this article, I decided to explain through simple code blocks gives more of the theory behind what that actually means. Let’s learn unit testing with simple examples!

Figure 1. Simple Division Function

Figure 2. Unit test for division function

In Figure 2, file contains our test method. As you can see in the code we have an assert method. Assert is useful in assessing whether a test passes or fails. There can be various types of assertions. For example, it can be boolean or null. JUnit has a class, named as assert which provides a bunch of assertion methods. This methods are useful in writing test cases to detect test failures. The first of these methods is the assertEquals() method which is in Figure 2. assertEquals() enables us to write a unit test that will compare a method with an expected and an actual value. It confirms the method works as expected. It checks if two objects are equal or not, if it’s not then throws an assertion error.

Figure 3. isEmpty method that returns a boolean value

Figure 4. Unit test of the isEmpty function

Code block which is in the Figure 3 written for Figure 4 test function. assertTrue () and assertFalse() assertions assert that the supplied condition is true or not. This enables us to check whether the condition is true based on the provided information.

Figure 5. Example of the arrays unit test

assertArrayEquals() checks whether the elements in the array are the same and in the same order.If elements orders are different, it throws an exception.

Figure 6. Parameterized unit test example

JUnit enabled the developer to test a single test method multiple times with different parameters. That means engineers writing a lot less code and it’s a lot more efficient. Parameterized tests are the same as other tests besides adding a new notation which is parameterized tests and value source to outline the different values that you want to test. It executes the same test over and over again using different values. Figure 6, shows that situation.

Learning unit testing is crucial if you want to create sophisticated software. Less likely occurrences or production of bugs may exist. As a result, the developers must create their own unit test scripts for every module and carry out testing themselves before delivering the code.

You can access entire code from the link below ↓

Java 102
Test Otomasyon


You need to log in to be able to comment!

Ece Özaydın


İstanbul, TR

© 2021 Patika Dev