How to Use JUnit parameterization test five steps of JUnit parameterization test:
(1) specify a special runtime org. JUnit. Runners. parameterized for the testing class that is preparing to use parametric tests.
(2) declare several variables for the test class to store the expected value and data for the test respectively.
(3) declare a public constructor with parameters for the test class, and assign values to the variables declared in the second step.
(4) declare an annotation Org. JUnit. runners. parameterized. parameters modifier. The returned value is Java. util. public static method of collection, and initialize all parameters to be tested in this methodNumber pair.
(5) Compile a test method and use the Defined variables as parameters for testing.
Package springmybatis. test; import Java. util. arrays; import Java. util. collection; import Org. JUnit. beforeclass; import Org. JUnit. test; import Org. JUnit. runner. runwith; import Org. JUnit. runners. parameterized; import Org. JUnit. runners. parameterized. parameters; import springmybatis. service. iuserservice; import springmybatis. util. factorymanager; import springmybatis. VO. user; // (1) Step 1: specify a special runtime Org. JUnit. runners. Parameterized @ runwith (parameterized. class) public class testspringmybatis {Private Static iuserservice userservice = NULL; @ beforeclasspublic static void setup () {factorymanager. init ("CONF/beans/beans-springMybatis.xml"); userservice = (iuserservice) factorymanager. getbean ("userservice");} // (2) Step 2: declare several variables for the test class to store the expected value and data used for the test. Here, I only put all the data for testing and did not set the expected value. Private int idparam; private string usernameparam; // (3) Step 3: declare a public constructor with parameters for the test class, and assign values to the variables declared in the second step. Public testspringmybatis (int id, string username) {This. idparam = ID; this. usernameparam = username;} // (4) Step 4: declare an annotation org for the test class. JUnit. runners. parameterized. parameters modifier. the return value is // Java. util. public static method of collection, and initialize all parameter pairs to be tested in this method. @ Parameterspublic static collection usernamedata () {return arrays. aslist (new object [] [] {1, "Jacky" },{ 2, "Andy" },{ 3, "Tomcat "},});} // (5) Step 5: Compile the test method and use the Defined variables as parameters for testing. @ Testpublic void testfindbyname () {system. out. println ("-------------"); User user2 = userservice. findbyname (usernameparam); system. out. println (user2) ;}// (5) Step 5: Compile the test method and use the Defined variables as parameters for testing. @ Testpublic void testfindbyid () {system. out. println ("**************"); User user1 = userservice. findbyid (idparam); system. out. println (user1 );}}
Output result:
-------------
User [userid = 1, account = 1, username = Jacky, Gender = male, Birthday = Fri Oct 11 09:00:00 CST 1963, email = jacky@xxx.net, address = Beijing, school = hongkongmusicshcol, company = hillstone, phone = 13900000000, Interest = movie]
************
User [userid = 1, account = 1, username = Jacky, Gender = male, Birthday = Fri Oct 11 09:00:00 CST 1963, email = jacky@xxx.net, address = Beijing, school = hongkongmusicshcol, company = hillstone, phone = 13900000000, Interest = movie]
-------------
User [userid = 2, account = 2, username = Andy, Gender = male, Birthday = SAT Oct 08 12:00:00 CST 1960, email = andy@xxx.net, address = Hongkong, school = hongkongdaxue, company = bigstar, phone = 13987654321, Interest = music]
************
User [userid = 2, account = 2, username = Andy, Gender = male, Birthday = SAT Oct 08 12:00:00 CST 1960, email = andy@xxx.net, address = Hongkong, school = hongkongdaxue, company = bigstar, phone = 13987654321, Interest = music]
-------------
User [userid = 3, account = 3, username = tomcat, Gender = male, Birthday = Fri Oct 01 14:00:00 CST 1965, email = tomcat@xxx.net, address = Hongkong, school = hongkongdaxue, company = bigstar, phone = 13912345678, Interest = programing]
************
User [userid = 3, account = 3, username = tomcat, Gender = male, Birthday = Fri Oct 01 14:00:00 CST 1965, email = tomcat@xxx.net, address = Hongkong, school = hongkongdaxue, company = bigstar, phone = 13912345678, Interest = programing]
Currently, only one parameter test is supported for JUnit.@ Parameters it would be even more powerful if multiple parameters are supported. However, as a unit testing tool, it has basically met the requirements of unit testing.
Complete Project Source code Address: http://download.csdn.net/detail/chenyechao/4646087