Junit Tutorial with example

JUnit is a Unit Testing framework for the Java Programming Language. This JUnit Tutorial is specially prepared for Java Developers who are absolute beginners and wants to have a good understanding of JUnit.

Prerequisites

  • Knowledge of Java.
  • A good understanding of Eclipse IDE.
  • More importantly a zeal to learn any technology/skills.

As a Software Developer, you know that we need to code the software as well as to test our source code. There is no way we can skip testing our own software. For this, we usually perform Unit Testing to test a chunk of code. So, in order to get started with JUnit let us first now take a brief look at Unit Testing.

What is Unit Testing?

Unit Testing is a type of software testing where the individual unit of code is tested. It checks whether the expected output is the same as the actual output.

  • Unit Testing is done by the developer to determine if any issue is there or not.
  • Here, the Software is divided into modules and then these modules are tested individually.

The main advantage of Unit Testing is that it exposes defects in the early phase of the life cycle of SDLC.

Why Unit Testing is important?

As we know, unit testing is a technique of testing individual modules or components. This will help us to verify the bugs or defects in the early phase of the life cycle of SDLC.

After unit testing, it will help both the developer and testers to save time as bugs are identified in the early phase.

Unit Testing is done using the following approach:

  • Manual Testing 
  • Automated Testing

Before going to JUnit let us distinguish between Manual as well as Automated Testing.

Manual vs Automation Testing

Now, there are lots of Automation tools available for Unit Testing a Software such as:

  • JUnit
  • TestNG
  • HtmlUnit
  • Cucumber, and etc.

What is JUnit?

JUnit is a Unit Testing framework for the Java Programming Language. With the help of JUnit, we write Unit tests in the Java programming language.

It comes up with a bundle of features that make it one of the popular and best frameworks for the Unit Testing Framework.

  • Firstly, it is an Open Source framework.
  • It comes up with lots of annotations.
  • It comes up with lots of small templates to test simple test cases.
  • There are Assertions to verify expected with actual results. 
  • It is simple and easy and takes less time.
  • JUnit being an automation tool will run automatically and will check the results on its own.

In a simple way, It is one of the best frameworks we have for Unit testing.

What is JUnit Testcase?

The purpose of Unit testing is to check whether we are getting the desired output or not. To achieve this, we will use a framework i.e JUnit.

Following is the Example for JUnit Testcase:

package in.codedec.junit.util;

import static org.junit.Assert.*;

import org.junit.Test;

public class StringUtilTest {

    @Test
    public void test() {
        assertEquals(125, 125);
    }

}

This is just an example to show how a test case is written. Here, we check whether both the expected output matches with actual output.

Don’t worry if you don’t understand the above example because in the next article of this tutorial we will do the Environment Set Up and will also understand this example in detail.

Thus, in this way, we saw the Overview of JUnit Framework which is a Unit Testing Framework for Java Programming Language.