一、一开始打开这个作业《测试与封装》的内容的时候,我都理解不了这个作业要如何操作,后来雪意发现了一个关于进行单元测试的网址:http://tips1000.com/archives/189.html,后来一边操作一边和雪意讨论,原来就是把自己原本的四则运算代码进行封装,在一个类里面运用多个方法,询问如果封装都要进行如此大的改动,那么单元测试不就很不简单了,老师说以后编写程序可以采取一个类里面多种方法,然后方便程序的封装与测试。我把我之前的四则运算程序改成了Calculator类里有四种方法(根据代码里用运算符分类来分出四种方法):public static void Onefhnum(int a,int b,int e)//一个运算符的方法、public static void Twofhnum(int a,int b,int c,int e,int h)//两个运算符的方法、public static void Threefhnum(int a,int b,int c,int d,int e,int h,int f)//三个运算符的方法、public static int getAnswer()//返回运算答案,然后在CalculatorTest类里加入数值来测试代码有无错误。
Calculator.java(封装)代码如下:
public class Calculator { static int answer; static char[]fh={'+','-','*','/'}; static char fh2; static char fh3; static char fh4; static int a,b,c,d,e,h,f; static int fhnum;//符号数 public static void Onefhnum(int a,int b,int e){//一个运算符的方法 fh2=fh[e]; if(fh2=='+'){ answer=a+b; } if(fh2=='-'){ answer=a-b; } if(fh2=='*'){ answer=a*b; } if(fh2=='/'){ answer=a/b; } } public static void Twofhnum(int a,int b,int c,int e,int h){//两个运算符的方法 fh2=fh[e]; fh3=fh[h]; if(fh2=='+') { if(fh3=='+'){ answer=a+b+c; } if(fh3=='-'){ answer=a+b-c; } if(fh3=='*'){ answer=a+b*c; } if(fh3=='/'){ answer=a+b/c; } } if(fh2=='-') { if(fh3=='+'){ answer=a-b+c; } if(fh3=='-'){ answer=a-b-c; } if(fh3=='*'){ answer=a-b*c; } if(fh3=='/'){ answer=a-b/c; } } if(fh2=='*') { if(fh3=='+'){ answer=a*b+c; } if(fh3=='-'){ answer=a*b-c; } if(fh3=='*'){ answer=a*b*c; } if(fh3=='/'){ answer=a*b/c; } } if(fh2=='/') { if(fh3=='+'){ answer=a/b+c; } if(fh3=='-'){ answer=a/b-c; } if(fh3=='*'){ answer=a/b*c; } if(fh3=='/'){ answer=a-b/c; } } } public static void Threefhnum(int a,int b,int c,int d,int e,int h,int f){//三个运算符的方法 fh2=fh[e]; fh3=fh[h]; fh4=fh[f]; if(fh2=='+')//第一为加 { if(fh3=='+') { if(fh4=='+') {answer=a+b+c+d;} if(fh4=='-') {answer=a+b+c-d;} if(fh4=='*') {answer=a+b+c*d;} if(fh4=='/') {answer=a+b+c/d;} } if(fh3=='-') { if(fh4=='+') {answer=a+b-c+d;} if(fh4=='-') {answer=a+b-c-d;} if(fh4=='*') {answer=a+b-c*d;} if(fh4=='/') {answer=a+b-c/d;} } if(fh3=='*') { if(fh4=='+') {answer=a+b*c+d;} if(fh4=='-') {answer=a+b*c-d;} if(fh4=='*') {answer=a+b*c*d;} if(fh4=='/') {answer=a+b*c/d;} } if(fh3=='/') { if(fh4=='+') {answer=a+b/c+d;} if(fh4=='-') {answer=a+b/c-d;} if(fh4=='*') {answer=a+b/c*d;} if(fh4=='/') {answer=a+b/c/d;} } } if(fh2=='-')//第一为减 { if(fh3=='+') { if(fh4=='+') {answer=a-b+c+d;} if(fh4=='-') {answer=a-b+c-d;} if(fh4=='*') {answer=a-b+c*d;} if(fh4=='/') {answer=a-b+c/d;} } if(fh3=='-') { if(fh4=='+') {answer=a-b-c+d;} if(fh4=='-') {answer=a-b-c-d;} if(fh4=='*') {answer=a-b-c*d;} if(fh4=='/') {answer=a-b-c/d;} } if(fh3=='*') { if(fh4=='+') {answer=a-b*c+d;} if(fh4=='-') {answer=a-b*c-d;} if(fh4=='*') {answer=a-b*c*d;} if(fh4=='/') {answer=a-b*c/d;} } if(fh3=='/') { if(fh4=='+') {answer=a-b/c+d;} if(fh4=='-') {answer=a-b/c-d;} if(fh4=='*') {answer=a-b/c*d;} if(fh4=='/') {answer=a-b/c/d;} } } if(fh2=='*')//第一为乘 { if(fh3=='+') { if(fh4=='+') {answer=a*b+c+d;} if(fh4=='-') {answer=a*b+c-d;} if(fh4=='*') {answer=a*b+c*d;} if(fh4=='/') {answer=a*b+c/d;} } if(fh3=='-') { if(fh4=='+') {answer=a*b-c+d;} if(fh4=='-') {answer=a*b-c-d;} if(fh4=='*') {answer=a*b-c*d;} if(fh4=='/') {answer=a*b-c/d;} } if(fh3=='*') { if(fh4=='+') {answer=a*b*c+d;} if(fh4=='-') {answer=a*b*c-d;} if(fh4=='*') {answer=a*b*c*d;} if(fh4=='/') {answer=a*b*c/d;} } if(fh3=='/') { if(fh4=='+') {answer=a*b/c+d;} if(fh4=='-') {answer=a*b/c-d;} if(fh4=='*') {answer=a*b/c*d;} if(fh4=='/') {answer=a*b/c/d;} } } } public static int getAnswer(){ return answer; } }
CalculatorTest.java(测试)代码如下:
import static org.junit.Assert.*; import junit.framework.Assert; import org.junit.Before; import org.junit.Test; public class CalculatorTest { @Before public void setUp() throws Exception { } @Test public void testOnefhnum() { Calculator.Onefhnum(1,2,0); Assert.assertEquals(3, Calculator.getAnswer()); } @Test public void testTwofhnum() { Calculator.Twofhnum(2,3,4,0,1); Assert.assertEquals(1, Calculator.getAnswer()); } @Test public void testThreefhnum() { Calculator.Threefhnum(4,5,5,6,0,0,0); Assert.assertEquals(20,Calculator.getAnswer()); } @Test public void testgetAnswer() { } }
测试情况如下:(成功)
二、白盒测试与黑盒测试:
1.白盒测试:
(1). testOnefhnum白盒测试(一个运算符,两个变量):
(2). testTwofhnum白盒测试(两个运算符,三个变量):
(3). testThreefhnum白盒测试(三个运算符,四个变量):
2.黑盒测试: