mercredi 1 mars 2017

JUnit test with exceptions

I have a implemented method and exception class :

class InsufficientFundsException extends RuntimeException { };

class AccountIdException extends RuntimeException { };

@Override
    public void deposit(Integer id, double amount) throws AccountIdException {
       try{
           for(Entry<Integer,Account> a : accounts.entrySet()){
                if(id.equals(a.getKey())) { 
                   double newBalance = a.getValue().getBalance() + amount;
                   a.getValue().setBalance(newBalance);
                }            
            }  
        }
        catch (AccountIdException e){
            System.out.println("Id konta jest nieprawidłowe : "+e );
        } 
    }`

and i have a test :

@Test(expected=AccountIdException.class)
public void testDeposit() {
    Double actualBalance = bank.accounts.get(1).getBalance();
    Double newBalance;
    assert actualBalance.equals(0.0);
    System.out.println(actualBalance);
    bank.deposit(1,1500.99);
    newBalance = bank.accounts.get(1).getBalance();
    assert newBalance.equals(1500.99)  ;
    System.out.println(newBalance);

}

And when i testing my method i received :

Expected exception: Bank.BankInt$AccountIdExceptionjunit.framework.AssertionFailedError

but deposit was added to account. What's wrong ???

Aucun commentaire:

Enregistrer un commentaire