reset password
Author Message
khsu
Posts: 30
Posted 15:44 Jan 18, 2014 |

Out of curiosity, how do I use write a Junit test for this class? (Given that I already have a working class for BanAccount.)

Here's my code.


    @Test
    public void testBankAccount() {
    {
        BankAccount dannisSaving = new BankAccount();
        dannisSaving.deposit(1000);
        dannisSaving.withdraw(500);
        dannisSaving.withdraw(400);
        assertEquals(100, dannisSaving.getBalance());
    }

 

I receive an error when i try to compile, which is expected, "Warning from last compilation assertEquals(double, double) in org.junit Assert has been deprecated.

Eric Liao
Posts: 158
Posted 15:52 Jan 18, 2014 |

For testing double type, you will need one more parameter as a threshold.

For example:

assertEquals(100, dnnisSaving.getBalance(), 0.5);

will be legal.

What it is doing is comparing 100 and dannisSaving.getBalance() to see if their difference is less than 0.5.

The reason we need to add this value is sometimes when you are doing calculation on the double, you might lose some precision.

ref:

http://junit.sourceforge.net/javadoc/org/junit/Assert.html#assertEquals%28double,%20double,%20double%29

khsu
Posts: 30
Posted 17:44 Jan 18, 2014 |

Interesting, thanks for clarifying that for me, I got it to work!

lakerfan94
Posts: 143
Posted 19:15 Jan 18, 2014 |

For this problem, do we need to create our own BankAccount class?

khsu
Posts: 30
Posted 11:44 Jan 19, 2014 |
lakerfan94 wrote:

For this problem, do we need to create our own BankAccount class?

Yes we do.

lakerfan94
Posts: 143
Posted 15:49 Jan 19, 2014 |

Thank you