保姆 Junit 说

Junit babysiter kata

我在 java/junit 上测试保姆套路时遇到了一些麻烦 运行。我的测试一直告诉我预期是 16,但实际上是 60。我不知道我的数学哪里出错了才能得到这个输出。我希望第二次测试的预期与我的实际相符。

public Object calculatePay() {
        int potentialPayBefore10 = 12;
        int potentialPayAfter10 = 8;

        //  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 所述,您的代码似乎是静态的,因此它对两个测试用例产生相同的结果。添加一个私有 class 变量来动态存储工资可能有助于通过测试

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;

    //  hour * 5 hours worked
    potentialPayBefore10 = 12 * hoursWorked;
    potentialPayAfter10 = 8 * hoursWorked;

    // TODO Auto-generated method stub

    if (potentialPayBefore10 < 60) {
        return potentialPayAfter10;
    } else
        return potentialPayBefore10;

}