5.2-5.3

【运行环境:】

        Eclipse  

第二阶段目标 - 通过测试程序和API 接口测试其简单的加减乘除功能。

第三阶段目标 - 通过测试程序和API 接口测试对于各种参数的支持。

被测试程序

 1 public class Zhengshu {
 2     int zs1,zs2,sum=1;int fh;
 3     double sum1;
 4     Zhengshu(){
 5         
 6     }
 7     Zhengshu(int Zs1,int Fh,int Zs2){
 8         zs1=Zs1;
 9         fh=Fh;
10         zs2=Zs2;
11     }
12     public void setJiaf(int Zs1,int Zs2)throws chaochuException
13     {
14         if(Zs1>1000||Zs2>1000)
15         {
16             throw new chaochuException("数值范围不正确!");
17         }
18         sum=Zs1+Zs2;
19     }
20     public void setJiaf(int Zs1,int Zs2,int zs3)throws chaochuException
21     {
22         if(Zs1>1000||Zs2>1000||zs3>1000)
23         {
24             throw new chaochuException("数值范围不正确!");
25         }
26         sum=Zs1+Zs2+zs3;
27     }
28     
29     public void setJianf(int Zs1,int Zs2)throws chaochuException
30     {
31         if(Zs1>1000||Zs2>1000)
32         {
33             throw new chaochuException("数值范围不正确!");
34         }
35         sum=Zs1-Zs2;
36     }
37     
38     public  void setChengf(int Zs1,int Zs2)throws chaochuException
39     {
40         if(Zs1>1000||Zs2>1000)
41         {
42             throw new chaochuException("数值范围不正确!");
43         }
44         sum=Zs1*Zs2;
45     }
46 
47     public void setChuf(int Zs1,int Zs2)
48     {
49         if(0==Zs2){
50             throw new ArithmeticException();
51         }
52         sum=Zs1/Zs2;
53     }
54 
55     public String getJiaf(){
56         return zs1+"+"+zs2;
57     }
58     public String getJianf(){
59         return zs1+"-"+zs2;
60     }
61     public String getChengf(){
62         return zs1+"*"+zs2;
63     }
64     public String getChuf(){
65         return zs1+"/"+zs2;
66     }
67     public int getSum(){
68         return sum;
69     }
70     public double getSum1(){
71         return sum1;
72     }
73     public String getsum(int zs1,int zs2){
74         return zs1+"/"+zs2;
75     }
76 }

单元测试程序

 1 import static org.junit.Assert.*;
 2 
 3 import org.junit.Test;
 4 
 5 
 6 public class ZhengshuTest {
 7     public static Zhengshu zs=new Zhengshu();
 8 
 9     @Test
10     public void testSetJiaf() {
11         try {
12             zs.setJiaf(1, 1);
13         } catch (chaochuException e) {
14             e.printStackTrace();
15         }
16         assertEquals(2,zs.getSum());
17     }
18     
19     public void testSetJiaf1() {
20         try {
21             zs.setJiaf(1, 1, 1);
22         } catch (chaochuException e) {
23             e.printStackTrace();
24         }
25         assertEquals(3,zs.getSum());
26     }
27 
28     @Test
29     public void testSetJianf() 
30     {
31         try {
32             zs.setJianf(1, 1);
33         } catch (chaochuException e) {
34             e.printStackTrace();
35         }
36         assertEquals(0,zs.getSum());
37     }
38 
39     @Test
40     public void testSetChengf() {
41         try {
42             zs.setChengf(1, 1);
43         } catch (chaochuException e) {
44             e.printStackTrace();
45         }
46         assertEquals(1,zs.getSum());
47     }
48     
49     
50 
51     @Test
52     public void testsetChuf() 
53     {
54             zs.setChuf(2, 2);
55             assertEquals(1,zs.getSum());
56         
57     }
58     
59     @Test(expected=ArithmeticException.class)
60     public void testsetChuf0() {
61             zs.setChuf(2, 0);
62     }
63 
64 
65 
66 }

自定义异常

1 class chaochuException extends Exception  
2 {  
3     public chaochuException(String msg)  
4     {  
5         super(msg);  
6     }  
7 }   

全部输入正确情况下的测试

对除数为零的异常测试

但运算数超过 1000 是就会抛出异常

方法重载

原文地址:https://www.cnblogs.com/linhaixin/p/4488032.html