我試着寫了下面的代碼JUnit測試代碼:爲什麼我對這組代碼的JUnit測試總是返回錯誤?
public class RateofPay {
public double totalSalary(double work, double work2, double hour1, double hour2, double hour3) {
double salary1 = work * hour1;
double salary2 = work2 * hour2;
double salary3 = work * hour3;
double wages = salary1 + salary2 + salary3;
return wages;
}
}
這是JUnit測試我已經寫了:
public class WageTest {
@Test
public void testWage() {
RateofPay test=new RateofPay();
double result=test.totalSalary(1.0, 2.0, 3.0, 4.0, 5.0);
assertEquals(12.0, result);
}
@Test
public void testWage1() {
RateofPay test=new RateofPay();
double result=test.totalSalary(6.0, 7.0, 8.0, 9.0, 10.0);
assertEquals(157.0, result);
}
}
的問題是,每一次當我運行JUnit測試,他們都沒有通過測試。結果說
assertEquals(預期的,實際的,增量)來比較浮點數。
我想知道什麼是錯?謝謝。
請分享失敗的日誌。 – nullpointer
看起來像數學是錯誤的!它應該是16和171! – Santo
關於三角洲的更多細節可在http://stackoverflow.com/questions/5939788/junit-assertequalsdouble-expected-double-actual-double-epsilon –