Junit4 Study Notes (2): parametric testing and Hypothesis (assumption)

Source: Internet
Author: User
Tags iterable

1. A simple test

Compile a calculator with only one operation:

 

1 public class Calculator {  2     public static double divide(int dividend, int divisor) {  3         return dividend / divisor;  4     }  5 }  

Compile a test for this method:

1 public class calculatortest {2 // allowable error 3 Private Static final Double Delta = 0.01; 4 5 @ Test 6 Public void testadd () throws exception {7 assertequals (3, calculator. divide (9, 3), delta); 8} 9}

In this test, the method is tested by dividing 9 by 3, but we want to use multiple groups of data for testing without writing multiple methods. In this case, the parameter test of JUnit can be used.

 

Ii. parametric Testing

In JUnit, there are two types of parameterized tests. The first form is the constructor form, that is, JUnit traverses all the parameters provided to call the constructor and execute the Test method:

1 // use parameterized runner to perform the parameterized Test 2 @ runwith (parameterized. class) 3 Public class calculatortest {4 // allowable error 5 Private Static final Double Delta = 0.01; 6 7 private int dividend; 8 private int divisor; 9 private int result; 10 11 Public calculatortest (INT dividend, int divisor, int result) {12 this. dividend = dividend; 13 this. divisor = divisor; 14 This. result = result; 15} 16 17 // @ parameterized. Parameters annotation indicates that this method returns all parameters. The annotated method must return 18 // The iterable object of the returned array. It must also be public and static. when the test is executed, the system traverses 19 // each group of parameters (arrays) to call the constructor and perform the test. 20 @ parameterized. parameters 21 public static iterable <object []> getparameters () {22 return arrays. aslist (new object [] [] {23 {9, 3, 3}, {5, 1, 5}, {12, 4, 3} 24 }); 25} 26 27 // after the test is executed, this method runs for 3 times 28 @ Test 29 public void testdevide throws exception {30 assertequals (result, calculator. divide (dividend, divisor), delta); 31} 32} 33

The second is the form of variable injection. The value of the variable is not initialized through the constructor, but is injected through JUnit:

1 // use parameterized runner to perform the parameterized Test 2 @ runwith (parameterized. class) 3 Public class calculatortest {4 // allowable error 5 Private Static final Double Delta = 0.01; 6 7 // use @ parameter to mark the public variable, JUnit traverses each group of parameters and injects the Integer Parameters in the 8 // annotation to indicate the number of parameters in the injection parameter group 9 @ parameter (0) 10 public int dividend; 11 @ parameter (1) 12 public int divisor; 13 @ parameter (2) 14 public int result; 15 16 // @ parameterized. parameters annotation this method returns all parameters. The annotated method must Return to 17 // load back the iterable object of the array, which must be public and static at the same time. When the test is executed, the system will traverse 18 // each set of parameters (array) call the constructor and perform the test. 19 @ parameterized. parameters 20 public static iterable <object []> getparameters () {21 return arrays. aslist (new object [] [] {22 {9, 3, 3}, {5, 1, 5}, {12, 4, 3} 23 }); 24} 25 26 // after the test is executed, this method runs 3 times 27 @ Test 28 public void testdivide () throws exception {29 assertequals (result, calculator. divide (dividend, divisor), delta); 30} 31}

 

Iii. Assumption

In the preceding parameterized example, if the parameters we provide are {9, 3, 3}, {15, 0, 0}, {12, 4, 3 }, the second set of parameters will cause the test to fail (15 divided by 0 will throw an exception). However, in parameterization testing, 0 should not be used as the test, so it should be a problem of test data, the test should not fail. Use Org. JUnit. various assume methods in assume can make assumptions about the test data or environment. If such assumptions are not met, skip the test to ensure that the test is executed in the correct test environment.

1 @ Test 2 Public void testdivide () throws exception {3 // assume that the divisor is not 0. If it is 0, skip this test 4 assumetrue ("divisor can't be 0", divisor! = 0); 5 assertequals (result, Calculator. Divide (dividend, divisor), delta); 6}

If you use the preceding parameters to perform the test, the test corresponding to the second set of parameters will be ignored, rather than fail.

This article is reproduced. If you have any copyright restrictions, you are willing to give up automatically.

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.