|
小弟刚开始学软件测试~
那天老师叫我们做个junit的测试我就随便拿了个简单例子
import junit.testEquation.*;
import junit.framework.TestCase;
public class EquationTest extends TestCase {
public EquationTest(String name) {
super(name);
}
public void testAccount() {
Equation e1 = new Equation(1, -2, 4);
Equation e2 = new Equation(1.0, -2.0, 1.0);
Equation e3 = new Equation(2, 12.3, 3);
e1.account();
e2.account();
e3.account();
assertEquals("无实数解", e1.getS());// 测试无实数解的情况
assertTrue((e2.getX1() - e2.getX2()) < 0.0001
&& (e2.getX1() - e2.getX2()) > -0.0001);
// 测试等根的情况;
assertTrue(e3.getX1() != e3.getX2());// 测试不等根的情况
}
}
------------------------------------
public class Equation {
double a;
double b;
double c;
double x1, x2;
String s;
public Equation(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
public void account() {
double dalt = b * b - 4 * a * c;
try {
if (dalt < 0) {
s = "无实数解";
System.out.println(s);
} else {
x1 = (-b + Math.sqrt(dalt)) / (2 * a);
x2 = (-b - Math.sqrt(dalt)) / (2 * a);
System.out.println(x1);
System.out.println(x2);
}
} catch (Exception e1) {
e1.getStackTrace();
}
}
public String getS() {
return s;
}
public double getX1() {
return x1;
}
public double getX2() {
return x2;
}
}
如上这个例子,但老师问了我句这样子测和不用junit直接运行程序看结果有区别么··那Junit的优势又体现在哪呢?我查了些资料了解junit是黑盒测试;但是要怎样运用才能体现出junit的特点呢~小弟是在茫然
比如上面的例子是不是太简单了!还请哪位高手指点下或者提供个好点的测试例子能体现junit特点的;因为这么课老师要求必须上台演示出来还要回答老师的疑问才能过····还请各位高手帮帮忙了 |
|