[size=xx-small][/size][color=red]package csdn;
public class River {
[color=red][/color][color=red][/color]
private double curWaterLevel;
private double warnningLevel;
public River() {
}
public River(double curWaterLevel, double warnningLevel) {
this.curWaterLevel = curWaterLevel;
this.warnningLevel = warnningLevel;
}
public void flow() throws Exception {
System.out.println(doFlow());
}
//doFlow保证flow方法可测试!flow直接打印doFlow()方法返回值
public String doFlow() throws Exception {
if (curWaterLevel - warnningLevel >= 10) {
throw new Exception("决堤啦");
}
return "安静的流";
}
public double getCurWaterLevel() {
return curWaterLevel;
}
public void setCurWaterLevel(double curWaterLevel) {
this.curWaterLevel = curWaterLevel;
}
public void setWarnningLevel(double warnningLevel) {
this.warnningLevel = warnningLevel;
}
public double getWarnningLevel() {
return warnningLevel;
}
}
//测试用例
//Java code
package csdn.junit;
import junit.framework.TestCase;
import csdn.River;
public class RiverNormalTestor extends TestCase {
public void testNormal() {
// 测试没有异常
try {
River river = new River(19, 10);
assertEquals("安静的流", river.doFlow());
} catch (Exception e) {
assertTrue(false);
}
}
public void testException() {
// 测试存在异常 1
try {
new River(20, 10).doFlow();
assertTrue(false);
} catch (Exception e) {
assertTrue(true);
}
// 测试存在异常 2
try {
new River(20, 10).doFlow();
assertTrue(false);
} catch (Exception e) {
assertTrue(true);
}
}
}[/color]
public class River {
[color=red][/color][color=red][/color]
private double curWaterLevel;
private double warnningLevel;
public River() {
}
public River(double curWaterLevel, double warnningLevel) {
this.curWaterLevel = curWaterLevel;
this.warnningLevel = warnningLevel;
}
public void flow() throws Exception {
System.out.println(doFlow());
}
//doFlow保证flow方法可测试!flow直接打印doFlow()方法返回值
public String doFlow() throws Exception {
if (curWaterLevel - warnningLevel >= 10) {
throw new Exception("决堤啦");
}
return "安静的流";
}
public double getCurWaterLevel() {
return curWaterLevel;
}
public void setCurWaterLevel(double curWaterLevel) {
this.curWaterLevel = curWaterLevel;
}
public void setWarnningLevel(double warnningLevel) {
this.warnningLevel = warnningLevel;
}
public double getWarnningLevel() {
return warnningLevel;
}
}
//测试用例
//Java code
package csdn.junit;
import junit.framework.TestCase;
import csdn.River;
public class RiverNormalTestor extends TestCase {
public void testNormal() {
// 测试没有异常
try {
River river = new River(19, 10);
assertEquals("安静的流", river.doFlow());
} catch (Exception e) {
assertTrue(false);
}
}
public void testException() {
// 测试存在异常 1
try {
new River(20, 10).doFlow();
assertTrue(false);
} catch (Exception e) {
assertTrue(true);
}
// 测试存在异常 2
try {
new River(20, 10).doFlow();
assertTrue(false);
} catch (Exception e) {
assertTrue(true);
}
}
}[/color]