EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

JUnit Parameterized Test

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
Home Software Development Software Development Tutorials JUnit Tutorial JUnit Parameterized Test

JUnit Parameterized Test

Introduction to JUnit Parameterized Test

JUnit Parameterized test facilitates developers to repeat the testing of code with a different set of input values in an automated method and saves considerable time in completing the test cycle.  Developers use parameters to store test data for various test runs and feed the data to the execution framework when the actual test is run.

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,064 ratings)

Using these kinds of tests, developers can direct the parameters to a variety of data sources and do the testing to verify the expected results. As an example, the data from the E-mail source or Message queue source, or ERP application scenario can be pointed to the parameters in the class, and all these scenarios can be tested easily.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

What are the components in Parameterized Tests?

Parameter

It is a variable that holds the test data, and it has a label or name which is unique within the code. It can store data under a variety of data types, i.e., numeric, alphanumeric, etc.  Parameters are defined during the test design stage, and they can be for exclusive use in a test or shared with across other tests.

Data Set

Data sets are the values passed to testing at any given point in time. These values are passed through parameters in the test runs. These data sets can be fed manually or through a file or interfaced with another system.

How to Create JUnit Parameterized Tests

The following steps are used to create Parameterized tests.

  1. Create a Class that has to be tested. An example of this class may be to input two numbers and multiply them and arrive at the result.
  2. Develop a parameterized test class. This test class should be annotated in the very beginning with @RunWith (Parameterized.class). It indicates the runner’s class name and the runner’s type. If nothing is specified, then the Default runner type is BlockJunit4ClasssRunner. Variables along with their data type, exclusive or shared details are defined in this test class. This class enables tests to run with a new instance, and it also invokes JUnit life cycle methods like setting up and tearing down to associate/release resources.
  3. A constructor should be created to store the test data. In addition, a placeholder should be provided for all the variables defined in the classes.
  4. A static method should be created to generate test data and return them to the classes. This should cover all the input variables defined in the classes. There are other methods to link the data sources directly from any other application.  Annotation @parameters  to generate necessary data for running the tests.
  5. For setting up the resource, the annotation @before should be used, and annotation @Test should be used to create the test. Finally, an assert statement can be used to verify the test results.
  6. Test Runner class should be created to execute the test. Junitcore and runclasses with test class names as parameters can be used in test cases execution. Finally, the result can be computed and displayed to the user.

JUnit Parameterized Test – Creation of Class

First of all, create a class that needs to be tested with the Parameters concept. Let’s take a simple class; it will take three numbers and compute the multiplied product of these numbers. The three numbers will also take the end value in the test case class and verify that the written code generates the same value computed through the logic built into it.

Class to be tested

package JunitCheck;
public Class  Multiplier {
public int product(int x, int y , int z) {
return x*y*z ;
}
}

Parameterized Test Class Creation

@RunWith (Parameterized.class)
public class MultiplierTest {
private int number1;
private int number2;
private int number3;
private int correctresult;
private Multiplier multiplier;

JUnit Parameterized Testing Example

An end to end coding of parameterized testing is given below

package JunitCheck;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import jave.util.Collection;
import org.junit.Before;
import.org.junit.Test;
import.org.runner.Runwith;
import.org.runners.Parameterized;
@RunWith (Parameterized.class)
public class MultiplierTest {
private int number1;
private int number2;
private int number3;
private int correctresult;
private Multiplier multiplier;
public MultiplierTest (int number1, int number2, int number3, int correctresult) {
super();
this.number1 = number1;
this.number2 = number2;
this.number3 = number3;
this.correctresult = correctresult;
}
@Before
public void initialize() {
multiplier = new Multiplier();
}
@Parameterized.parameters
public static Collection input() {
return Arrays.aslist(new Object[ ][ ] { {1, 2, 3, 6} , {2, 4, 5, 40} , {6, 6, 6, 216) ,
{7, 3, 4, 84} , {4, 4, 4, 64});
}
@Test
public void testMultiplierTest() {
System.out.println(“Multiplied Product of 3 numbers = : “ + correctResult );
assert.Equals( correctResult, multiplier.product(number1, number2, number3 ));
}

Test Runner class has to be created to run the Parameterized test

Package JunitCheck;

import org.junit.runner.Result;
import org.junit.runner.JUnitCore;
import org.juit.runner.notification.Failure;
public class Test {
public static void main(String[ ] args) {
Result result = JUnitCore.runclasses(MultiplierTest.class);
For (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.oout.println(result.wasSuccessful());
}
}

Explanation of the above code

  • The “Public static void main” method declares the first execution line for the JUnit test.
  • Junit core run classes facilitate the execution of test cases using its runclasses. Test class name will be used as a parameter. Multiplier.class will be used as a parameter in our example.
  • Results will be processed in a loop using the data, and the output will be compared with passed values, and the result will be declared as success or failure based on the verification.
  • Either of the results will be published to the user.

Conclusion

JUnit Parameterized tests are easy to comprehend, develop and manage. The same test can be executed with multi various inputs in quick time. Effectively it reduces the total number of test cases that the developer may have to create, and it brings down the manual effort. This helps developers to organize the tests/results and avoids duplicate testing. The main advantage of this method is that it facilitates Devops to be implemented easily in an organization due to its agility in testing the whole code set quickly and enabling continuous integration of codes in the production environment.

Recommended Articles

This is a guide to JUnit Parameterized Test. Here we discuss What are the components in Parameterized Tests along with How to Create Parameterized Tests. You may also have a look at the following articles to learn more –

  1. JUnit Annotations 
  2. JUnit version
  3. JUnit Maven Dependency
  4. Junit Interview Questions
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

Special Offer - All in One Software Development Bundle (600+ Courses, 50+ projects) Learn More