JUnit测试用例代码:
1 import org.junit.Test;
2 import java.math.BigDecimal;
3 import junit.framework.TestCase;
4 public class caculate_test extends TestCase {
5     private Caculator ca = new Caculator();
6     protected void setUp() throws Exception {
7         super.setUp();
8     }
9     @Test
10     public void test_Add() {
11         ca.setNum1(1);
12         ca.setNum2(2);
13         ca.Add();
14         assertEquals("3.0", ca.getResult());
15         ca.setNum1(2.4);
16         ca.setNum2(1.2);
17         ca.Add();
18         assertEquals("3.6", ca.getResult());
19         ca.setNum1(Double.MAX_VALUE);
20         ca.setNum2(2);
21         ca.Add();
22         assertEquals("ERROR", ca.getResult());
23         }
24     @Test
25     public void test_Sub() {
26         ca.setNum1(1);
27         ca.setNum2(2);
28         ca.Sub();
29         assertEquals("-1.0", ca.getResult());
30         ca.setNum1(2.4);
31         ca.setNum2(1.2);
32         ca.Sub();
33         assertEquals("1.2", ca.getResult());
34         ca.setNum1(-Double.MAX_VALUE);
35         ca.setNum2(2);
36         ca.Sub();
37         assertEquals("ERROR", ca.getResult());
38         }
39     @Test
40     public void test_Mul() {
41         ca.setNum1(1);
42         ca.setNum2(2);
43         ca.Mul();
44         assertEquals("2.0", ca.getResult());
45         ca.setNum1(2.4);
46         ca.setNum2(1.2);
47         ca.Mul();
48         assertEquals("2.88", ca.getResult());
49         ca.setNum1(1.3);
50         ca.setNum2(0);
51         ca.Mul();
52         assertEquals("0.0", ca.getResult());
53         ca.setNum1(Double.MAX_VALUE);
54         ca.setNum2(2);
55         ca.Mul();
56         assertEquals("ERROR", ca.getResult());
57         }
58     @Test
59     public void test_Div() {
60         ca.setNum1(1);
61         ca.setNum2(2);
62         ca.Div();
63         assertEquals("0.5", ca.getResult());
64         ca.setNum1(2.4);
65         ca.setNum2(1.2);
66         ca.Div();
67         assertEquals("2.0", ca.getResult());
68         ca.setNum1(1.3);
69         ca.setNum2(0);
70         ca.Div();
71         assertEquals("ERROR", ca.getResult());
72         }
73 }
  测试结果:

  GitHub:https://github.com/TongWee/SoftwareTesting_Caculator/