mercredi 28 novembre 2018

Java - JUnit testing for a method when else statement outputs error message as String

I'm working on a program that processes bank transactions. My withdraw method subtracts the amount from the balance and if there aren't enough funds for the withdrawal, an error message is outputted to the screen. The error message is the case I am having trouble testing.

public void withdraw(double amt) {
    double bal = getBalance();
    if(bal >= amt) {
        super.setBalance(bal - amt);
        if(bal < minBalance) {
            super.setBalance(bal - amt - maintFee);
        }   
    } else {
        System.out.println("Error: Not enough funds for withdraw");
    }       
}

These are my JUnit tests for this method currently. I just need help on testWithdrawThree(). Thank you!

@Test
void testWithdrawOne() {
    Savings s = new Savings(500.00, 30.00, "111", "Andrew Green", 1000.00);
    s.withdraw(200);
    assertEquals(800.00, s.getBalance());
}

@Test
void testWithdrawTwo() {
    Savings s = new Savings(500.00, 30.00, "111", "Andrew Green", 400.00);
    s.withdraw(200.00);
    assertEquals(170.00, s.getBalance());
}

@Test
void testWithdrawThree() {
    Savings s = new Savings(500.00, 30.00, "111", "Andrew Green", 400.00);
    s.withdraw(600.00);
    //Need to check that program will output "Error: Not enough funds for withdrawal"

}

Aucun commentaire:

Enregistrer un commentaire