Reputation: 66
How to test this Fraction constructor in Junit4 test case? Thank you!
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());
}
}
}
Upvotes: 0
Views: 6880
Reputation: 312404
I'd run the constructor and then check the denominator holds the value you expect:
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
}
Upvotes: 3