Home Ask Login Register

Developers Planet

Your answer is one click away!

Michelle 3 weeks ago

InsufficientFundsException with JUNIT test

I built an Account class that have 3 properties: balance, owner, acctNo. The Account class have 2 constructors, one that takes 3 properties and one that takes no data. The Account class have set and get methods as well as deposit and withdraw methods.

public class Account {
private int acctNo;
private String owner;
private int balance;

public Account() {
    acctNo = 0;
    owner = "";
    balance = 0;
}

public Account(int no, String own, int bal) {
    this.acctNo = no;
    this.owner = own;
    this.balance = bal;
}

public int getAcctNo() {
    return acctNo;
}

public void setAcctNo(int no) {
    this.acctNo = no;
}

public String getOwner() {
    return owner;
}

public void setOwner(String own) {
    this.owner = own;
}

public int getBalance() {
    return balance;
}

public void setBalance(int bal) {
    this.balance = bal;
}

public void withdraw(int amt) throws InsufficientFundsException {
    if(amt <= balance) {
        balance -= amt;
    }
    else {
        int newBalance = amt - balance;
        throw new InsufficientFundsException(newBalance);
    }

}

public void deposit(int amt) {
    this.balance += amt;
}

public static void main (String args[]) {
    Account ac = new Account(1234, "david", 15000);
    try {
        ac.withdraw(1500);
    }catch(InsufficientFundsException e)
    {
        System.out.println("Account number: " +ac.getAcctNo());
        System.out.println("owner: " +ac.getOwner());
        System.out.println("Balance is :" +ac.getBalance() );
    }
}

}

I also built an InsufficientFundsException class that extend from the Exception class. After modify this class to Account class, the withdraw method or this setBalance method attempts to set the balance below zero, the InsufficientFundsException will be thrown.

public class InsufficientFundsException extends Exception {

private final int amount;

public InsufficientFundsException(int amt) {
    this.amount = amt;
        

Answers


Simulant 3 weeks ago

you withdraw 0 from your account in your test. This is OK and does not throw the expected exception so your test fails as you expect the Exception to be thrown.

set the amount = 1; in your test so you withdraw more than your account has in balance and the Exception will be thrown and your test will be OK as the expected Exception is thrown.


davidhxxx 3 weeks ago

Your test fixture is not correct.
Because if account.balance==0 and amt==0, this condition is true :

 if(amt <= balance) {

So InsufficientFundsException is never thrown.

Your test could be written :

@Test(expected=InsufficientFundsException.class)
public void withdrawWithNotEnoughFund() throws InsufficientFundsException {
    int amount = 1;
    Account instance = new Account();
    instance.withdraw(amount);
    int balance = instance.getBalance();
}


GhostCat 3 weeks ago

Here:

if(amt <= balance) {
    balance -= amt;

You only throw that exception when AMT is bigger than the current balance.

But in your test, you set balance to 0, and amt ... as well. 0 == 0, thus no exception is thrown.

Post Status

Asked in 3 weeks ago
Viewed 3,994 times
Voted 12
Answered 3 times

Search




Leave an answer


Quote of the day: live life