我在java/junit上为保姆kata运行测试时遇到了一些问题。我的测试一直告诉我,期望值是16,但实际上是60。我不知道我的数学在哪里出错了,才能得到这个输出。我希望我的期望值与我的第二次测试的实际值相匹配。
public Object calculatePay() {
int potentialPayBefore10 = 12;
int potentialPayAfter10 = 8;
// $12 hour * 5 hours worked
potentialPayBefore10 = 12 * 5;
potentialPayAfter10 = 8 * 2;
// TODO Auto-generated method stub
if (potentialPayBefore10 < 60) {
return potentialPayAfter10;
} else
return potentialPayBefore10;
}
}
public class DaysWorked {
/*
* Story: As a babysitter In order to get paid for 1 night of work I want to
* calculate my nightly charge
*/
// Project Goal: Create test to show Mellie being paid
// Start with calc time for 1 hour of work
@Test
public void calculatepayforworkafterstarttimeat12hourly() {
// 5 is hours worked if start at 5 pm til 10p
MellieWageCalculator potentialPay = new MellieWageCalculator(5);
// assert equals gives -> (expected, actual)
assertEquals(60, potentialPay.calculatePay());
}
@Test
public void calculatepayforworkafter10pmat8hourly() {
// 2 hours worked if start at 10pm til 12 pm
MellieWageCalculator potentialPay = new MellieWageCalculator(2);
assertEquals(16, potentialPay.calculatePay());
}
}发布于 2019-03-14 04:03:23
正如@sirandy提到的,您的代码似乎是静态的,因此它会为两个测试用例产生相同的结果。添加一个私有类变量来动态存储工资可能有助于通过测试
private int hoursWorked= 0;
public MellieWageCalculator(int i) {
// TODO Auto-generated constructor stub
this.hoursWorked= i;
}
public Object calculatePay() {
int potentialPayBefore10 = 12;
int potentialPayAfter10 = 8;
// $12 hour * 5 hours worked
potentialPayBefore10 = 12 * hoursWorked;
potentialPayAfter10 = 8 * hoursWorked;
// TODO Auto-generated method stub
if (potentialPayBefore10 < 60) {
return potentialPayAfter10;
} else
return potentialPayBefore10;
}https://stackoverflow.com/questions/55149916
复制相似问题