JUnit 4 Tutorial 6 – Parameterized Test
The “Parameterized Test” means vary parameter value for unit test. In JUnit, both @RunWith and @Parameter annotation are use to provide parameter value for unit test, @Parameters have to return List[], and the parameter will pass into class constructor as argument.
import java.util.Arrays; import java.util.Collection; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; /** * JUnit Parameterized Test * @author mkyong * */ @RunWith(value = Parameterized.class) public class JunitTest6 { private int number; public JunitTest6(int number) { this.number = number; } @Parameters public static Collection<Object[]> data() { Object[][] data = new Object[][] { { 1 }, { 2 }, { 3 }, { 4 } }; return Arrays.asList(data); } @Test public void pushTest() { System.out.println("Parameterized Number is : " + number); } }
Result
Parameterized Number is : 1 Parameterized Number is : 2 Parameterized Number is : 3 Parameterized Number is : 4
It has many limitations here; you have to follow the “JUnit” way to declare the parameter, and the parameter has to pass into constructor in order to initialize the class member as parameter value for testing. The return type of parameter class is “List []”, data type has been limited to String or primitive value.

there is a simpler way. check zohhak.googlecode.com it lets you write:
Nice post. BTW, it seems that is supporting any data time:
thanks very much.. this really helped me!
Hi ,
I want to know that how we can implement different assertion for each parameter in same test case.
As the collection of object is passed to constructor of Class. for every test run i want to have different assertion but that is not possible in current example.
Thanks in advance.
Hi,
Could you please help me out regarding below issue:
First Test case failure is carried to other consecutive tests when I used Parameterized tests. How can I skip this error:
Below is the snippet of the code:
Just remove the try and catch construct in test method
issue found in the above code is :
1) junit4 doesn’t have (Not worked for me)
2) private int fExpected;
an unnecessary variable
3) Exception handling in the test case is not appreciated …
good to use like below
Hello Ramesh,
I think the issue is that you have ‘static’ declared in your @Rule.
create error collector instance without the ‘static’ keyword and it should work fine in Junit 4.
Hi ,
Am new to Junit Testing and getting Initialization error while running Test class . pls help in resolving it , Thanks in advance
code :
package com.source;
import java.util.Arrays;
import java.util.Collection;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class ParameterizedAddclassTest {
public int a[];
public int expected;
ParameterizedAddclassTest(int inputparameters[],int expected)
{
this.a=inputparameters;
this.expected=expected;
}
@Parameterized.Parameters
public static Collection parameterPassing()
{
return Arrays.asList(new Object[][]{ {new int[]{5,2,3},10} ,
{new int[] {5,10,15},30}
});
}
@Test
public void TestaddClass()
{
Addclass add =new Addclass();
int actual=add.addClass(a);
System.out.println(actual);
assertEquals(actual,expected);
}
}
error :
java.lang.AssertionError: expected: but was:
at org.junit.Assert.fail(Assert.java:58)
at org.junit.Assert.failNotEquals(Assert.java:259)
at org.junit.Assert.assertEquals(Assert.java:80)
at org.junit.Assert.assertEquals(Assert.java:88)
at org.junit.runners.Parameterized$TestClassRunnerForParameters.getOnlyConstructor(Parameterized.java:100)
at org.junit.runners.Parameterized$TestClassRunnerForParameters.(Parameterized.java:80)
at org.junit.runners.Parameterized$RunAllParameterMethods.(Parameterized.java:115)
at org.junit.runners.Parameterized.(Parameterized.java:140)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:27)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.(JUnit4TestReference.java:29)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:40)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:30)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
I am trying to subclass a Junit class to be used a ‘harness’ for parameterizing tests. I Want to subsclass a class that looks like
@RunWith(Parameterized.class)
public class A {
@Parameters
public Collection data() {
return overrideableMethod();
}
}
Subclass
public class B extends A {
@Override
public Object[] overrideableMethod() {
// change list of parmeters
}
@Test a() {
}
}
However, this seems not to be possible due to Java treating the @Parameters annotation.
My main objective is to simplify subclasses so that they don’t have to use the @Parameters and @RunWith annotations, and automatically get benefit of some standard parameters built into the base class
Any tips / ideas?
How do you make it jdk 1.5 (or above) compliance? I’m getting: The expression of type List needs unchecked conversion to conform to Collection
I’m using jdk1.6, has no warning at the above code.
May be you can explicit convert the List to collection by using the following code,
@Parameters
public static Collection
Did you use tTestNG before? Personally i more prefer to use TestNG as my unit test framework.
http://www.mkyong.com/unittest/junit-4-vs-testng-comparison/