在Java中更改值

时间:2016-11-05 04:47:56

标签: java debugging junit

我正在尝试将值传递给Bill类中的setAmount()方法,并确保可以在方法中更改它们。我正在使用JUnit来测试该方法。我不确定该方法中的隐私泄漏是什么意思。我确保构造函数中没有隐私泄漏但不确定我将如何修复它。失败的一行是:

assertTrue( b1.setAmount(m2));

中的

testSetAmountOKWithPrivacyLeak()

@Test
public void testSetAmountOKWithPrivacyLeak()
{
    Date d1 = new Date( 3,4,2020);
    Money m1 = new Money(10);
    Bill b1 = new Bill( m1, d1, "fred");

    Money m2 = new Money(11);
    // Not paid so we can chane it successfully
    assertTrue( b1.setAmount(m2));
    // Check money changed. 
    assertTrue( b1.getAmount().equals( m2));

    //Change money  in n2 and verify it has not changed. 
    m2.setMoney(12,0);
    assertFalse( b1.getAmount().equals(m2));
}

//Change the amount owed.
//If already paid returns false and does not change the amount owed else changes 
//the amount and returns true.
public boolean setAmount (Money amount) {
    if (isPaid() == false) {
        amount = new Money(amount);
        return true;
    }
    else {
        return false;
    }
}

//returns true if bill is paid, else false
public boolean isPaid () {
    return (paidDate != null);
}

0 个答案:

没有答案