Unit Testing

Unit testing is process of Software development which validate that each unit/line of code is performing as expected or not. On broader prospect Unit Testing is defect prevention and detection strategy to reduce software development risks, cost, and time to deliver a quality software.

Developers write Unit Test cases which covers the functionality of specific section of code, function level, class level and branches level so that ensure the implemented code is working as expected. Unit testing not completely verify the functionality of software but it’ ensure that building block of software work independently as expected from others.

Why Unit Testing Framework?

In Unit Testing, we verify each section of code for success and failure scenarios where need to implement the conditions in code Test code by considering the different cases.

Let’s take the below examples of Calculator.java class on addition() method where need to verify output of result by considering different test cases for success and failure.


public class Calculator {
		public int addition(int a, int b) {
			return a + b;

Test Cases Implementation

In this case for testing this method we need to create one test class i.e CaculatorTest.java and implement the condition for all scenarios of every method test as follows:

Success Case: Pass arguments as a=2, b=3 return result=5, Expected Result =5

Failed Case: Pass arguments as a=2, b=3 return result=Other, Expected Result=5

class CalculatorTest {
private static final Logger logger = Logger.getLogger(CalculatorTest.class.toString());
private Calculator calc = new Calculator();
		void additionTest() {
		int sum = calc.addition(2, 3);
		if (sum == 5) {
		logger.info("Addition method test has passed.");
		} else {
		logger.info("Addition method test has failed.");
		public static void main(String[] args) {
			CalculatorTest cTest = new CalculatorTest();

Here if you noticed for testing a single small addition() method required to write these many lines of code for creating object of Calculator class, calling method and check for pass and failed scenarios. Apart from it you need monitor the console logs also for verify the expected result. Think of when there are multiple methods in class and software having lots of classes on that case writing Junit code is cumbersome process for developers.

From the above example, Now analyze tasks that need to be done manually with and without framework :

TaskWithout FrameworkWith Framework
PreparationManually Manually
Provide Test Inputs Manually Manually
Run the tests Manually Framework
Provide expected outputs Manually Manually
Verify results Manually Framework
Notify developer if test failed Manually Framework

Now it should be clear that how important to use framework because it saves significant amount of time of developers.

There are lots of framework for Unit Testing but when we talk about the Java unit testing mainly these two are most popular.

  1. Junit
  2. TestNG

TestNG is a testing framework for Java and is inspired by JUnit and NUnit which covers a wide range of testing like unit testing, functional testing, integration testing, and system testing.

In the further topics we will discuss more about the Junit basics and implementation of test cases by using Junit 5 after considering different scenarios.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s