1.实验目的:
1)理解单元测试原理
2)学会使用Junit做单元测试
2.实验方法:
1)确定测试单元
2)设计测试用例
3实验内容:
1)编写四则运算程序
2)确定测试单元
3)设计测试用例
4)使用Junit做单元测试
4.具体实验:
1)编写四则运算程序
package javaapplication1;
public class MathClass {
private double x1;
private double x2;
private double result;
public void plus(double para1,double para2){
result = para1 + para2;
}
public void minus(double para1,double para2){
result = para1 - para2;
}
public void multiply(double para1,double para2){
result = para1 * para2;
}
public void divide(double para1,double para2){
result = para1 / para2;
}
public double getResult(){
return result;
}
}
2)确定测试单元
@Test
public void testPlus() {
System.out.print("plus: ");
double para1 = 1.6;
double para2 = 1.2;
MathClass instance = new MathClass();
instance.plus(para1, para2);
// TODO review the generated test code and remove the default call to fail.
assertEquals(2.8, instance.getResult(), 0.00001);
System.out.println(instance.getResult());
}
/**
* Test of minus method, of class MathClass.
*/
@Test
public void testMinus() {
System.out.print("minus: ");
double para1 = 1.6;
double para2 = 1.2;
MathClass instance = new MathClass();
instance.minus(para1, para2);
// TODO review the generated test code and remove the default call to fail.
assertEquals(0.4, instance.getResult(), 0.00001);
System.out.println(instance.getResult());
}
/**
* Test of multiply method, of class MathClass.
*/
@Test
public void testMultiply() {
System.out.print("multiply: ");
double para1 = 2.6;
double para2 = 1.2;
MathClass instance = new MathClass();
instance.multiply(para1, para2);
// TODO review the generated test code and remove the default call to fail.
assertEquals(3.12, instance.getResult(), 0.00001);
System.out.println(instance.getResult());
}
/**
* Test of divide method, of class MathClass.
*/
@Test
public void testDivide() {
System.out.print("divide: ");
double para1 = 3.12;
double para2 = 2.6;
MathClass instance = new MathClass();
instance.divide(para1, para2);
// TODO review the generated test code and remove the default call to fail.
assertEquals(1.2, instance.getResult(), 0.00001);
System.out.println(instance.getResult());
}
3)设计测试用例
plus: 1.6+1.2
minus:1.6-1.2
multiply:2.6*1.2
divide:3.12/2.6