JUnit 5 and Unit Testing

  • It is an open-source testing framework for java programmers.

  • It is one of the unit testing frameworks. Current version is JUnit 5.

  • Process of checking the functionality of Application whether it is working as per requirements.

  • Testing of single entity (Class or Method) is called as unit testing.

  • To perform unit testing we need to create test cases.

  • The unit test case code ensures that the program logic works as expected.

  • Manual : Executing test case without any tool. Time consuming, less reliable.

  • Automated : Executing test case with tool such as Junit 5. It is fast and more reliable.

Consider the class JbkAdd having method add. Now we have to perform testing on add method, whether it is working correct or not.

Now we will make separate Test class for JbkAdd as JbkManualTest and we will write test case for add method.

Requirements – Junit5.jar, Extend TestCase class

Follow Following Steps –

  • Add Junit jar into build path as –Right click on Project-> Properties-> Libraries-> Add Library-> JUnit-> Junit-5-> Ok.

Adding JUnit Library to Java Project

Write Following code for JUnit Testing –

Consider the class JbkAdd having method add. Now we have to perform JUnit testing on add method, whether it is working correct or not.


Now we will make separate Test class for JbkAdd as JbkJunitTest and we will write test case for add method which would be positive.


Running JUnit Test

Now we will make separate Test class for JbkAdd as JbkJunitTest and we will write test case for add method which would be Negative.


Running JUnit Test

Now we will make separate Test class for JbkAdd as JbkJunitTest and we will write test case which consists of assertEquals as well as assertNotSame.