EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials JUnit Tutorial JUnit Runner
Secondary Sidebar
JUnit Tutorial
  • Junit
    • JUnit 5 Assert
    • JUnit RunWith
    • JUnit AssertThat
    • JUnit Report
    • JUnit BeforeClass
    • JUnit Framework
    • JUnit Data Provider
    • JUnit Runner
    • JUnit 5 Parameterized Tests
    • JUnit BeforeAll
    • JUnit Private Methods
    • JUnit Integration Test
    • JUnit Maven Dependency
    • JUnit Annotations?
    • JUnit Testing
    • JUnit Test Order
    • JUnit 5 RunWith
    • JUnit 4 Maven
    • JUnit Jar
    • JUnit assertEquals
    • JUnit 5 Parameterized Test
    • JUnit Dependency
    • JUnit Fail
    • JUnit Disable Test
    • JUnit Assert
    • JUnit in Maven
    • JUnit 5 Gradle
    • JUnit XML
    • JUnit XML Format
    • JUnit Eclipse
    • JUnit Test Suite
    • JUnit Parameterized Test
    • JUnit assert exception
    • JUnit Code Coverage
    • JUnit Jupiter
    • JUnit Rule
    • JUnit version
    • Junit Interview Questions

JUnit Runner

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,950 ratings)

JUnit Runner

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:

JUnit Runner 1

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:

@RunWith annotation

JUnit Runner 3

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

JUnit Runner 4

In this step, we extract the downloaded project and open the same by using the spring tool suite.

we are extracting the downloaded project

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.

we are checking all the project structure

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:
JUnit Runner 7

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:

java application

JUnit Runner 9

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 –

  1. JUnit Code Coverage
  2. JUnit assertEquals
  3. JUnit Jupiter
  4. JUnit 5 Maven Dependency
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more