如何在Junit4测试用例中测试此Fraction构造函数? 谢谢!
public class Fraction {
private int num;
private int den;
public Fraction(int num,int den){
this.num=num;
this.den=den;
if(den==0){
this.den=1;
System.out.println("ERROR: DENOMINATOR CAN NOT BE 0");
System.out.println("Now the fraction is changed to"+toString());
}
}
}
答案 0 :(得分:3)
我运行构造函数,然后检查分母是否包含您期望的值:
public class FractionTest() {
private Fraction f;
@Test
public void testLegalConstruction() {
f = new Fraction (4, 7);
assertEquals ("wrong num", 4, f.getNum());
assertEquals ("wrong den", 7, f.getDen());
@Test
public void testIlegalConstruction() {
f = new Fraction (3, 0);
assertEquals ("wrong num", 3, f.getNum());
assertEquals ("wrong den", 1, f.getDen()); // Note the 1
}