March 20, 2020
Estimated Post Reading Time ~

What is JUNIT Test Framework with some example?

JUnit is a Regression Testing Framework used by developers to implement unit testing in Java and accelerate programming speed and increase the quality of code.
So what is Regression Testing Framework?
Regression testing is a type of software testing that verifies that software that was previously developed and tested still performs correctly after it was changed or interfaced with other software. Changes may include software enhancements, patches, configuration changes, etc.
It is an open-source testing framework for Java programmers. The java programmer can create test cases and test his/her own code.
It is one of the unit testing frameworks. The current version is JUnit 4.
To perform unit testing, we need to create test cases. The unit test case is a code which ensures that the program logic works as expected.
To perform a Junit test we have to add two external jars file in your Java project
1.    junit-4.xx.jar
2.    hamcrest-core-1.x jar
To understand it more clearly I am going to put here a simple example
1.Calculator.java
1.  package com.demo.junit;
2.   
3.  public class Calculator {
4.          public int add(int xint y)
5.          {
6.                 return x+y;
7.          }
8.         
9.          public int sub(int xint y)
10.        {
11.               return x-y;
12.        }
13.}
Now the unit test file
2. TestUnit. java
1.  package com.demo.junit;
2.   
3.  import static org.junit.Assert.*;
4.   
5.  import org.junit.Test;
6.   
7.  public class TestUnit {
8.          @Test
9.          public void testAdd() {
10.               Calculator calculator = new Calculator();
11.               assertEquals("Result ", 5, calculator.add(2, 3));
12.        }
13.        @Test
14.        public void testSub()
15.        {
16.               Calculator calculator=new Calculator();
17.               assertEquals("Result ", 1,calculator.sub(6,5));
18.        }
19.}
Explanation: The Junit 4.x framework is annotation based, so let's see the annotations that can be used while writing the test cases.
@Test annotation specifies that the method is the test method.
@Test(timeout=1000) annotation specifies that the method will be failed if it takes longer than 1000 milliseconds (1 second).
@BeforeClass annotation specifies that method will be invoked only once, before starting all the tests.
@Before annotation specifies that the method will be invoked before each test.
@After annotation specifies that the method will be invoked after each test.
@AfterClass annotation specifies that the method will be invoked only once, after finishing all the tests.
So basically this is how the output will look like
This case in the snap is a successful one. So actual value is equal to the expected value. So this test case is a success.
The output of a failure case
If any of one case from any number of cases will fail it will show a failure.
Source:
2.TutorialsPoint: JUnit Test Framework
3.Regression Testing: Regression testing
Hope you will find this useful. 


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.