Updated February 17, 2023
Introduction to JUnit Runner
JUnit runner is nothing but the class used to extend the junit runner’s abstract class. It is used to run the test cases; we can run the test cases by using @RunWith annotation with junit runner. It is an elementary class that implements the descriptive interface; we can use junit runner to create the test suite in our java application.
To use it in our application, we need to add the junit dependency in our project. Without adding junit dependency, we cannot use it in our code. It will run the tests and notifies the runnotifier for the specified event. We need a subclass of runners when using RunWith annotation for invoking the Runner, which is custom. When creating a custom runner while implementing abstract methods, we need to provide a constructor that takes it as an argument. The junit default runner will guarantee that the instance of the test class is constructed as soon as possible before running the test on the application. The same Runner will retain no reference with an instance of the test class.
Running the tests one by one is a panic task for the developer; also, it is not manageable in any project. It will contain different classes and methods. To overcome this situation, the framework of junit will give the ability to call the test method, which is called the runner test. While annotating our class with @RunWith or extending the class with @RunWith junit will invoke the class, it will reference running the test class instead of a runner built into the junit.
How does JUnit Runner Works?
It will initiate the test classes running into the test methods and found from the classes and report the test results.
As we know, junit runner is an elementary class that implements the interface. The Runner class contains the two abstract methods as follows.
Code:
public abstract class Runner (abstract class) implements describable
{
public abstract Description (abstract method) getDescription ();
public abstract void (abstract method) run (RunNotifier rnotifier);
}
The above getDescription method will be inherited from the describable and then return the description. The description will contain the information which we are using. All junit test runners will extend the abstract class from the runner class. We can use the runner class of the test by using annotation as @Runwith. If our test class does not contain this annotation, then junit will use the default test runner. The default test runner is called as BlockJUnit4ClassRunner.
We are using a run () method, which is a very generic method. A runner is not the class that we are extending. Instead, there is two different abstract class that we are using for it, the first is the child abstract class, and the second is the parent abstract class. Parent runner is an abstract class that is more specific and contains more children in one class. In a parent-runner class, the test is structured and executed to be hierarchical. First, the junit subclass is needed to return the generic type of children runner. The parent runner will then ask the subclass to create a description of each child’s class and then need to run each child’s class.
The below example shows how runners will work in junit as follows.
In the below example, we are creating a class name as JunitRunner.
In the example below, we create a single class that defines junit runner.
Code:
public class JunitRunner {
return p + q;
}
}
Output:
In the example below, we define its second class as JunitRunner1. This code describes the @RunWith annotation with the class name as JunitRunner. Also, we are using the testAdd method to add the numbers. We are also using @Test annotation and assertEquals methods.
Code:
@RunWith (JunitRunner.class)
public class JunitRunner1 {
Calc cal = new Calc();
@Test
public void testadd () {
System.out.println ("Addition");
assertEquals ("add", 8, cal.add (15, 13));
}
}
Output:
Using JUnit Runner
The JUnit test runner is nothing but the test runner class, which will run a single test or group of tests together. The class is nothing but the primary method called junit framework core class.
Below steps shows how to use junit runner as follows:
We are creating a project name as JunitRunner. In this step, we make the project template of junit runner in spring boot. We provide the project group name as com. For example, the artifact name is JunitRunner, the project name is JunitRunner, and the selected java version is 11. We are defining the version of spring boot as 2.7.0
Group – com.example
Artifact name – JunitRunner
Name – JunitIgnore
Spring boot – 2.7.0
Project – Maven
Java – 11
Package name – com.example. JunitRunner
Project Description – Project for JunitRunner
In this step, we extract the downloaded project and open the same by using the spring tool suite.
In this step, we check all the project structure and its files. Also, we are checking whether that pom.xml file is created or not. If this file is not created, we need to make the same manually. However, this file is created in the below example, so we do not need to create it manually.
In this step, we are adding the junit dependency to the project. We are adding junit dependency.
Code:
<dependencies>
<dependency>
<groupId> org.junit.jupiter </groupId>
<artifactId> junit-jupiter-engine </artifactId>
<version> 5.3.1 </version>
<scope> junit- ignore </scope>
</dependency>
<dependency>
<groupId> junit </groupId>
<artifactId> junit </artifactId>
<version> 4.12 </version>
</dependency>
</dependencies>
Output:
After adding the dependency, the below example shows how to use it in the java application.
Code:
@RunWith (JUnitRunner.class)
public class JunitRunner2 {
Calc cal = new Calc ();
@Test
public void testAddition () {
assertEquals ("Add", 19, cal.add(15, 13));
}
}
Output:
Conclusion
It is an elementary class that implements the interface, which is describable; we can use it to create the test suite in our java application. It is nothing but the class used to extend the junit runners, abstract class.
Recommended Articles
This is a guide to JUnit Runner. Here we discuss the introduction; how does JUnit runner work? And how to use junit runner for better understanding. You may also have a look at the following articles to learn more –