Introduction
JUnit is a framework in Java used for unit testing. It is vital in the development of test-driven development. It is part of the xUnit framework. We link the JUnit as a JAR file at compile time. JUnit5 resides under the org.junit.jupiter package in Java.
import org.junit.jupiter.api.*;Execution Procedure defines the order of methods called in JUnit Testing. This section will discuss the Execution Procedure for a JUnit Test API.
“Read This Topic, procedure call in compiler design“
Execution Procedure
First, we will create the TestJUnit.java file in our environment. I will be using NetBeans 8.0 for this demonstration.
TestJUnit.java
package Package1;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
/**
*
* @author 91963
*/
public class TestJUnit {
@BeforeClass
public static void beforeClass() {
System.out.println("This is the Before Function");
}
@AfterClass
public static void afterClass() {
System.out.println("This is the After Function");
}
@Before
public void before() {
System.out.println("This is the Before Each Function");
}
@After
public void after() {
System.out.println("This is the After Each Function");
}
//test case 1
@Test
public void testCase1() {
System.out.println("This is Test Case 1");
}
//test case 2
@Test
public void testCase2() {
System.out.println("This is Test Case 2");
}
}Next, we will create our Runner class,
TestRunner.java
package Package1;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
/**
*
* @author 91963
*/
public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestJUnit.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}The structure of our environment should look like this,

The final obtained output will be,

In the above example, we have described the Execution Procedure of JUnit Testing by taking the example of before, after, beforeEach, and afterEach JUnit methods.
Check out JUnit Interview Questions here.



