Five steps for JUnit parametric testing:
(1) Specify a special runner org.junit.runners.Parameterized for the test class that is ready to use the parameterized test.
(2) Declare several variables for the test class, respectively, to hold the expected value and the data used for the test.
(3) Declare a public constructor with parameters for the test class and assign values to several variables declared in the second link.
(4) Declare a public static method for the test class that uses the annotation org.junit.runners.Parameterized.Parameters to return a value of java.util.Collection. and initialize all parameter pairs that need to be tested in this method.
(5) Write a test method and test it with the defined variables as parameters.
Import Staticorg.junit.Assert.assertEquals;Importjava.util.Arrays;Importjava.util.Collection;ImportOrg.junit.Before;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;Importorg.junit.runners.Parameterized;Importorg.junit.runners.Parameterized.Parameters;//(1) Step one: The test class specifies a special runner org.junit.runners.Parameterized@RunWith (parameterized.class) Public classCalculatortest {PrivateCalculator Cal; //(2) Step two: Declare several variables for the test class, respectively, to store the expected value and the data used for the test. Private intexpected; Private intinput1; Private intInput2; @Before Public voidsetUp () {cal=NewCalculator (); } //(3) Step three: Declare a public constructor with parameters for the test class and assign values to several variables declared in the second link. PublicCalculatortest (intExpected,intINPUT1,intINPUT2)//constructs a method that assigns values to individual parameters. { This. Expected =expected; This. INPUT1 =input1; This. Input2 =Input2; } //(4) Step four: Declare for the test class A org.junit.runners.Parameterized.Parameters decorated with annotations, with a return value of//java.util.Collection The public static method, and in this method initializes all parameter pairs that need to be tested. @Parameters Public StaticCollection<integer[]>Preparedata () {integer[][] object= {3, 1, 2}, {-4,-1,-3}, {5, 0, 5 } }; returnArrays.aslist (object);//The array is converted into a collection form. } //(5) Step five: Write a test method that uses the defined variables as parameters for testing. @Test Public voidTestadd () {System.out.println ( This. input1+ "," + This. Input2); Assertequals ( This. Expected, Cal.add ( This. INPUT1, This. Input2));//Note the member variable that is called. } }
Output:1,2-1,-30,5
http://blog.csdn.net/seven_3306/article/details/8069948
How to use JUnit parametric testing