JUNIT4 parameterized settings and test suite use

Source: Internet
Author: User
Tags constructor

JUNIT4 parameterization Setup is necessary:

To ensure the rigor of unit tests, it is common to simulate different test data to test the processing capability of the method.
For this we need to write a lot of unit testing methods, but these test methods are very similar
Their code structure is the same, and the difference is just the test data and expectations
You can use JUNIT4 's parametric test to extract the same code from the test method and improve the code reuse

JUNIT4 parameterized set Instance code (from Mu-class network):

Import static org.junit.assert.*;
Import java.util.*;
Import Org.junit.Test;
Import Org.junit.runner.RunWith;
Import org.junit.runners.Parameterized;
Import Org.junit.runners.Parameterized.Parameters; /* 1. Change the default test runner above the parameterized settings class to Runwith (Parameterized.class) * 2. Declare a variable to hold the expected value and the result value * 3. Declare a public static method that returns a value of collection and use the @paramet ERs for retouching * 4. Declare a public constructor with parameters for the test class, and declare the variable assignment in it */@RunWith (Parameterized.class)//Change the default test runner to Runwith (
	Parameterized.class) public class Parametertest {int expected = 0;//declare variable to hold expected value and result value int input1 = 0;
	
	int input2 = 0; @Parameters//Declare a public static method with a return value of Collection, and use the @parameters to decorate the common static collection<object[]> T () {return ARR
	Ays.aslist (new object[][]{{3,1,2}, {4,2,2}}); }//Declare a public constructor with parameters for the test class and assign a value for the variable that it declares to be parametertest (int expected,int input1,int input2) {this.expected = E
		xpected;
		THIS.INPUT1 = INPUT1;
	This.input2 = Input2; } @Test//write test methods using defined variables as parameters to test public void Testadd () {assertequals (expected, new CaLculate (). Add (INPUT1, input2));
 }
}

JUNIT4 parameterization setting points:

(1) test classes ready to use parametric tests must be decorated by the org.junit.runners.Parameterized runner

(2) Declaring several variables for the test class, respectively, for storing the expected value and the data used for the test
(3) Prepare the data. The preparation of the data needs to be done in a single method, the requirements that the method needs to meet are as follows
1) This method must be modified by org.junit.runners.Parameterized.Parameters annotations
2) The method must be a public static method that returns a value of type Java.util.Collection
3) The method has no parameters and the method name is arbitrary . And the method is executed before the class is instantiated.
(4) Declare a public constructor with parameters for the test class and assign a value to the variable declared in (2)
(5) Writing test methods, using defined variables as parameters for testing

JUNIT4 parameterization Setting Disadvantages:

In general, only one test method is executed within a class. Because the data being prepared is not shared
This requires that the method to be tested is a method of large data volume, so it is necessary to write a parameterized test
and in actual development, parameterized tests are not used very much

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.