How to use JUnit parametric testing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.