Jun babysiter说

问题描述 投票:2回答:1

我在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());

    }

}
java testing junit calculator
1个回答
0
投票

正如@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;

}
© www.soinside.com 2019 - 2024. All rights reserved.