我在 java/junit 上运行保姆套路测试时遇到了一些麻烦。我的测试一直告诉我预期是 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());
}
}
最佳答案
正如 @sirandy
提到的,您的代码似乎是静态的,因此它对两个测试用例产生相同的结果。添加一个私有(private)类变量来动态存储工资可能有助于通过测试
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;
}
关于java - Junit保姆套路,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55149916/