java单元测试

 1 package com.bxw.demo;
 2 
 3 public class Calculator {
 4      private static int result; // 用于存储运行结果
 5         public void add(int n) {
 6             result = result + n;
 7         }
 8         public void substract(int n) {
 9             result = result - 1;  // result =result-n
10         }
11         public void multiply(int n) {
12         }         // 此方法尚未完成
13         public void divide(int n) {
14             result = result / n;
15         }
16         public void square(int n) {
17             result = n * n;
18         }
19         public void squareRoot(int n) {
20             for (; ;) ;            //死循环
21         }
22         public void clear() {     // 将结果清零
23             result = 0;
24         }
25         public int getResult() {
26             return result;
27         }
28 }
demo

以上代码用来做java的单元测试。

 1 package com.bxw.test;
 2 
 3 import static org.junit.Assert.*;
 4 
 5 import org.junit.Before;
 6 import org.junit.Ignore;
 7 import org.junit.Test;
 8 
 9 import com.bxw.demo.Calculator;
10 
11 /**
12  * 单元测试demo
13  * 需要在要测试的方法加@Test标注,表明这是一个测试方法。
14  * .@Ignore表明这是一个未完成方法,跳过测试
15  * .@Before在任何一个测试执行之前必须执行的代码
16  * .@After在任何一个测试执行之后必须执行的代码
17  * .@Test(timeout=1000)限时测试
18  * @author Administrator
19  *
20  */
21 public class CalculatorTest {
22     //创建一个待测试的类
23      private static Calculator calculator = new Calculator();
24         
25         @Before
26         public void setUp() throws Exception {
27             calculator.clear();
28         }
29         //异常测试
30         @Test(expected  =  ArithmeticException. class )
31         public void divideByZero(){
32             calculator.divide(0); 
33        } 
34 
35         @Test
36         public void testAdd() {
37             calculator.add(2);
38             calculator.add(3);
39             assertEquals(5, calculator.getResult());
40         }
41 
42         @Test
43         public void testSubstract() {
44             calculator.add(10);
45             calculator.substract(2);
46             assertEquals(8, calculator.getResult());
47         }
48 
49         @Ignore("Multiply() Not yet implemented")
50         @Test
51         public void testMultiply() {
52         }
53 
54         @Test
55         public void testDivide() {
56             calculator.add(8);
57             calculator.divide(2);
58             assertEquals(4, calculator.getResult());
59         }
60 
61 }
test1

这是CalculatorTest.java的单元测试

 1 package com.bxw.test;
 2 
 3 import static org.junit.Assert.*;
 4 
 5 import java.util.Arrays;
 6 import java.util.Collection;
 7 
 8 import org.junit.Before;
 9 import org.junit.Test;
10 import org.junit.runner.RunWith;
11 import org.junit.runners.Parameterized;
12 import org.junit.runners.Parameterized.Parameters;
13 
14 import com.bxw.demo.Calculator;
15 /**
16  * 参数化测试
17  * 类前加RunWith
18  * @author bxw
19  *
20  */
21 @RunWith(Parameterized.class)
22 public class SquareTest {
23     private static Calculator calculator = new Calculator();
24     private int param;
25     private int result;
26     @Before
27     public void setUp() throws Exception {
28         calculator.clear();
29     }
30 //提供测试数据
31     @SuppressWarnings("rawtypes")
32     @Parameters
33     public static Collection data(){
34         return Arrays.asList(new Object[][]{
35             {2,4},
36             {0,0},
37             {-3,9}
38         });
39     }
40     //constructor
41     public SquareTest(int param, int result) {
42         this.param = param;
43         this.result = result;
44     }
45     @Test
46     public void test() {
47         calculator.square(param);
48         System.out.println(param+" "+result);
49         assertEquals(result, calculator.getResult());
50     }
51 
52 }
参数化测试

参数化测试,测试数据写成二维数组的形式。

 1 package com.bxw.test;
 2 
 3 import static org.junit.Assert.*;
 4 
 5 import org.junit.Before;
 6 import org.junit.Test;
 7 import org.junit.runner.RunWith;
 8 import org.junit.runners.Suite;
 9 
10 /**
11  * 打包测试
12  * @author bxw
13  *
14  */
15 @RunWith(Suite.class)
16 @Suite.SuiteClasses({
17     CalculatorTest.class,
18     SquareTest.class
19 })
20 public class AllTest {
21     
22 }
打包测试

 将测试类写在@Suite.SuiteClasses中

原文地址:https://www.cnblogs.com/popcornya/p/6774172.html