Junit4.x test runner
JUnit provides the default test runner for unit tests. It is responsible for executing all testing methods.
We can also customize our own runners, all of which are inherited fromOrg. JUnit. Runner. Runner
You can also useOrg. JUnit. runer. runwith AnnotationSpecify a specific runner for each test class
Generally, the default test runner can meet the requirements of most unit tests.
When you use some of the advanced features provided by JUnit or customize the JUnit Testing Method for specific needs
Explicit declaration of test run is essential
Create a junit4.x test suite
①Create an empty class as the entrance to the test suite
②UseOrg. JUnit. Runner. runwithAndOrg. JUnit. Runners. Suite. suiteclasses AnnotationModify this empty class
③SetOrg. JUnit. Runners. SuiteThe runwith annotation is passed in as a parameter, that is, this class is executed using the kit runner
④An array of test classes to be put into this test suite as a parameter for suiteclasses Annotation
⑤Ensure that this empty class is usedPublicAnd there are public constructors without any parameters.
The following is the sample code for creating a test suite class in junit4.x.
Package COM. jadyer. junit4; </P> <p> Import Org. JUnit. runner. runwith; <br/> Import Org. JUnit. runners. suite; <br/> Import Org. JUnit. runners. suite. suiteclasses; </P> <p>/** <br/> * example of the junit4.x test suite <br/> * @ see calculatortest. class and parametertest. the classes are self-compiled unit test classes of junit4 <br/> */<br/> @ runwith (suite. class) <br/> @ suiteclasses ({calculatortest. class, parametertest. class}) <br/> public class testall {}
The following is the sample code for creating a test suite class in junit3.8.
Package COM. jadyer. junit3; </P> <p> Import JUnit. framework. test; <br/> Import JUnit. framework. testcase; <br/> Import JUnit. framework. testsuite; </P> <p>/** <br/> * Run all test classes in batches in junit3.8 .. Run as JUnit test directly on this class <br/> * @ see here the typical combination mode in the design mode is used, combine different things <br/> * @ see after the combination, that is, it can contain itself, it can also contain a part of it <br/> * @ see testsuite itself is composed of testcase, then testsuite can contain testcase <br/> * @ see and testsuite can continue to contain testsuite, form a recursive relationship <br/> * @ see is shown here, so this is a very good design model, A good policy <br/> */<br/> public class testall extends testcase {<br/> // The method name must be public static test suite () <br/> Public static test s Uite () {<br/> // testsuite class implements the test interface <br/> testsuite suite = new testsuite (); <br/> // The Class Object of the test class is passed here. This method can also receive objects of the testsuite type <br/> suite. addtestsuite (calculatortest. class); <br/> suite. addtestsuite (mystacktest. class); <br/> return suite; <br/>}< br/>}
Parameterization test of junit4.x
To ensure the rigor of unit testing, different test data is often simulated to test the processing capability of the method.
Therefore, we need to write a lot of unit testing methods, but these testing methods are similar.
Their code structures are the same. The difference is only the test data and expectations.
In this case, the parameter test of junit4 can be used,Extract the same code from the test method,Improve code reuse
Junit3.8 has no good solutions for such problems,Junit4.x makes up for junit3.8's shortcomings.
Key Points of parametric Testing
①The test classes to be tested using parameterization must be composedOrg. JUnit. Runners. parameterizedOperator Modifier
②Prepare data. Data preparation must be performed in a method. The requirements for this method are as follows:
1)This method must beOrg. JUnit. Runners. parameterized. Parameters AnnotationModify
2)This method must beThe returned value is Java. util. collection.TypePublic staticMethod
3)This methodNo Parameters,Method names are optional. This method is executed before the class is instantiated.
③For test classDeclare SEVERAL VARIABLESUsed to store the expected value and the data used for testing.
④DeclareCommon constructor with ParametersAnd③Variable value declared in
⑤Compile the test method and use the Defined variables as parameters for testing.
Disadvantages of parametric Testing
Generally, only one test method is executed in a class. Because the prepared data cannot be shared.
This requires that the method to be tested is a large amount of data, so it is necessary to write a parameterized test.
In actual development, parameterized tests are not used in many aspects.
The following is the sample code for parametric testing in junit4.x.
The first is calculator. Java
Package COM. jadyer. junit4; </P> <p>/** <br/> * mathematical computing --> addition <br/> */<br/> public class calculator {<br/> Public int add (int, int B) {<br/> return a + B; <br/>}< br/>}
Then there is the parameter test class parametertest. Java of junit4.x.
Package COM. jadyer. junit4; </P> <p> Import static Org. JUnit. assert. assertequals; // static import </P> <p> Import Java. util. arrays; <br/> Import Java. util. collection; </P> <p> Import Org. JUnit. test; <br/> Import Org. JUnit. runner. runwith; <br/> Import Org. JUnit. runners. parameterized; <br/> Import Org. JUnit. runners. parameterized. parameters; </P> <p> Import COM. jadyer. junit4.calculator; </P> <p>/** <br/> * parameterization test of junit4 <br/> */< Br/> @ runwith (parameterized. class) <br/> public class parametertest {<br/> private int expected; <br/> private int input11; <br/> private int input22; </P> <p> Public parametertest (INT expected, int input11, int input22) {<br/> This. expected = expected; <br/> This. input11 = input11; <br/> This. input22 = input22; <br/>}</P> <p> @ parameters <br/> Public static collection preparedata () {<br/> // The type of the Two-dimensional array must be an object class. <Br/> // the data in the two-dimensional array is the add () <br/> // The data in each element of the Two-dimensional array corresponds to the Construction Method parametertest () parameter location in <br/> // Therefore, It is determined based on the parameter location of the constructor, the first data in the first element of the Two-dimensional array is equal to the sum of the last two data. <br/> // for specific usage rules, see Org. JUnit. runners. parameterized class description <br/> object [] [] object = {3, 1, 2}, {0, 0}, {-4,-1,-3 }, {6,-3, 9 }}; <br/> return arrays. aslist (object); <br/>}</P> <p> @ test <br/> Public void testadd () {<br/> calculator Cal = new Calculator (); <br/> assertequals (expected, Cal. add (input11, input22 )); <br/>}< br/>/*********************** [execution of the test process ]************************************** ********************************/<br/>/ /1 .. the preparedata () method is executed first, and the prepared data is returned as a collection. <br/> // 2 .. next, call the constructor Method Based on the prepared data. There are several elements in the collection, and this constructor will be called several times <br/> // here, there are 4 elements in the collection, so the parametertest () constructor will be called four times, therefore, four objects of the test class are generated. <br/> // for each object of the test class, testadd () is executed () method <br/> // while the data in the collection is transmitted by JUnit to parametertest (INT expected, int input11, int input22) constructor <br/> // testadd () the three private parameters used are set by the parametertest () constructor, the three values are from collection <br/> /************************** **************************************** **************************************** **/