JUnit multi-parameter test

Source: Internet
Author: User
Tags date1

A few days ago, we used JUnit to perform a unit test to test a method that passed in multiple parameters. It took a long time to get familiar with the unit test.

Here we have tried two methods for testing. As a newbie, many of them need to be improved.

Example method:

public void add(Long a,Long b,String c,String d,Date date1,Date 2){...}

Assume that the values of A and B are {-123,}, and the values of C and D are {null, "", "sdfsdf "}, the values of data1 and data2 are {null, new data (), new date (new date. get time ()-24*3600), new date (new date. gettime () + 24*3600 )}

In order to overwrite each case, a possible combination of each parameter is required. There are 4*4*3*3*4*4 = 2304 possibilities, which need to be traversed. 

1. parameterization Test 

Five steps for 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 a public static method using the annotation org. JUnit. Runners. parameterized. Parameters for the test class and return the value of Java. util. collection,
In this method, all parameter pairs to be tested are initialized.
(5) Compile a test method and use the Defined variables as parameters for testing.

Import static Org. JUnit. assert. fail; import Java. util. arraylist; import Java. util. collection; import Java. util. date; import Java. util. list; 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 partest {long a; long B; string C; string D; Date date1; Date date2; // constructor with Parameters Public partest (long a, long B, string C, string D, date date1, date date2) {super (); this. A = A; this. B = B; this. C = C; this. D = D; this. date1 = date1; this. date2 = date2;} @ parameters public static collection <?> Preparedata () {return Ptest. filled () ;}@ test public void test () {// call Method Add (a, B, c, d, date1, date2 );}}

 Parameter combination: 

import java.util.ArrayList;import java.util.Date;import java.util.List;public class Ptest {    public static void main(String[] args) {        filled();    }    public static List<Object[]> filled() {        Long a[] = { -1l, 0l, 1l, 2l };        Long b[] = { -1l, 0l, 1l, 2l };        String[] c = { null, "123", "sfsfdsf" };        String[] d = { null, "123", "sfsfdsf" };        Date[] date1 = { null, new Date(),                new Date(new Date().getTime() - 24 * 3600) };        Date[] date2 = { null, new Date(),                new Date(new Date().getTime() + 24 * 3600) };        List<Object[]> resultList = diff(a, b, c, d, date1, date2);        return resultList;    }    public static List<Object[]> diff(Object[]... a) {        List<List<Object>> f = getFirstList(a[0]);        for (int i = 1; i < a.length; i++) {            f = diff(f, a[i]);        }        List<Object[]> resultList = new ArrayList<Object[]>();        for (int i = 0; i < f.size(); i++) {            Object[] o = new Object[f.get(i).size()];            for (int j = 0; j < f.get(i).size(); j++) {                o[j] = f.get(i).get(j);            }            resultList.add(o);        }        return resultList;    }    public static List<List<Object>> getFirstList(Object[] first) {        List<List<Object>> result = new ArrayList<List<Object>>();        for (Object i : first) {            List<Object> b = new ArrayList<Object>();            b.add(i);            result.add(b);        }        return result;    }    public static List<List<Object>> diff(List<List<Object>> array, Object[] a) {        List<List<Object>> result = new ArrayList<List<Object>>();        for (List<Object> as : array) {            for (Object i : a) {                List<Object> b = new ArrayList<Object>();                b.addAll(as);                b.add(i);                result.add(b);            }        }        return result;    }}

2. Package and PASS Parameters 

Package suite; import JUnit. framework. junit4testadapter; import JUnit. framework. test; import JUnit. framework. testcase; import JUnit. framework. testsuite; public class testcasesuit {public static test suite () {testsuite suite = new testsuite (); // create a test suite. addtest (New junit4testadapter (testparam. class); // Add the Class Object of the test class for (INT I = 0; I <10; I ++) {// number of tests testparam TP = new testparam ("add"); // specify the test method during construction and execute a test TP with the default parameter. setparam (a, B, c, d, date1, date2); // test the input parameter suite. addtest (TP); // perform the test of the input parameter} return suite ;}}

 

Package suite; import Java. util. arraylist; import Java. util. date; import Org. JUnit. test; import JUnit. framework. testcase; public class testparam extends testcase {// test data parameter long a; long B; string C; string D; Date date1; Date date2; // The constructor with parameters public partest (long a, long B, string C, string D, date date1, date date2) {super (); this. A = A; this. B = B; this. C = C; this. D = D; this. date1 = date1; this. date2 = date2;} public testparam (string name) {// todo auto-generated constructor stub super (name) ;}@ test public void test () {// call Method Add (a, B, c, d, date1, date2 );}}

 

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.