我正在尝试为以下代码编写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(expected,actual,delta)来比较浮点数。
我想知道出了什么问题?谢谢。
答案 0 :(得分:4)
在尝试比较delta
个数字时,您应该指定double
参数。
此外,你的断言是错误的。
他们应该是这样的。
@Test
public void testWage() {
Solution test=new Solution();
double result=test.totalSalary(1.0, 2.0, 3.0, 4.0, 5.0);
assertEquals(16.0, result, 0.0);
}
@Test
public void testWage1() {
Solution test=new Solution();
double result=test.totalSalary(6.0, 7.0, 8.0, 9.0, 10.0);
assertEquals(171.0, result, 0.0);
}
例如,第一个测试将进行此计算:
double salary1 = 1 * 3; //3
double salary2 = 2 * 4; //8
double salary3 = 1 * 5; //5
double wages = salary1 + salary2 + salary3; // 3 + 8 + 5 = 16