Example code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
package main.com.bankapp; /** * Created by rpchurchill on 9/3/2015. */ public class bankAccountClass { //HashSet<Object> accountList; String firstName, lastName; double balance; private EmailNotifier emailNotifier; private daoBankAcctDatabase bankDB; public bankAccountClass() { setBalance(0.0); setFirstName(""); setLastName(""); } public bankAccountClass(String first, String last, double initbal) { setFirstName(first); setLastName(last); setBalance(initbal); } public bankAccountClass(String first, String last, double initbal, EmailNotifier emailNotify) { setFirstName(first); setLastName(last); setBalance(initbal); emailNotifier = emailNotify; } public bankAccountClass(String first, String last, double initbal, daoBankAcctDatabase acctDB) { setFirstName(first); setLastName(last); setBalance(initbal); bankDB = acctDB; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public void setLastName(String lastName) { this.lastName = lastName; } public void setBalance(double balance) { this.balance = balance; } public String getLastName() { return lastName; } public double getBalance() { return balance; } public void deposit(double depamount) { setBalance(getBalance() + depamount); } //TODO figure out hoow to throw and test for excepptions public void withdrawal(double wdamount) { //throws acctOverdraftException { if (wdamount > getBalance()) { // throw new acctOverdraftException() emailNotifier.sendMessage("fred", "barney", "wilma", "betty"); } else deposit(-wdamount); } public double sumDeposits(int days) { double[] deposits; double sum = 0.0; deposits = bankDB.getDepositsFromDatabase(days); for (double d : deposits) { sum += d; } return sum; } } |
Example tests for above:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
package test.unit.com.bankapp; import junit.framework.Assert; import main.com.bankapp.EmailNotifier; import main.com.bankapp.bankAccountClass; import main.com.bankapp.daoBankAcctDatabase; import org.junit.Test; import static org.mockito.Mockito.*; /** * Created by rpchurchill on 9/3/2015. */ public class bankAccountClassTest { @Test //does account exist public void acctExists() { bankAccountClass account = new bankAccountClass("Fred","Flintstone",0.0); Assert.assertNotNull(account); Assert.assertEquals(0.0, account.getBalance(), 0.00000001); Assert.assertEquals("Fred", account.getFirstName()); Assert.assertEquals("Flintstone",account.getLastName()); } @Test public void acctDepositSimple() { bankAccountClass account = new bankAccountClass("Fred","Flintstone",0.0); account.deposit(100.0); Assert.assertEquals(account.getBalance(), 100.0, 0.00000001); } @Test public void acctDepositCents() { bankAccountClass account = new bankAccountClass("Fred","Flintstone",0.0); account.deposit(43.42); Assert.assertEquals(account.getBalance(), 43.42, 0.00000001); } @Test public void acctDepositModifyExisting() { bankAccountClass account = new bankAccountClass("Fred","Flintstone",0.0); account.deposit(250.24); account.deposit(100.0); Assert.assertEquals(account.getBalance(), 350.24, 0.00000001); } @Test public void acctDepositModifyExisting1() { bankAccountClass account = new bankAccountClass("Fred","Flintstone",100.0); account.deposit(25); Assert.assertEquals(account.getBalance(), 125.0, 0.00000001); account = new bankAccountClass("Fred","Flintstone",23.42); account.deposit(100); Assert.assertEquals(account.getBalance(), 123.42, 0.00000001); account = new bankAccountClass("Fred","Flintstone",12.41); account.deposit(55.00); Assert.assertEquals(account.getBalance(), 67.41, 0.00000001); account = new bankAccountClass("Fred","Flintstone",67.89); account.deposit(10.25); Assert.assertEquals(account.getBalance(), 78.14, 0.00000001); } @Test public void acctDepositModifyMultiple() { bankAccountClass account = new bankAccountClass("Fred","Flintstone",0.0); account.deposit(45.45); account.deposit(50.00); account.deposit(12.73); Assert.assertEquals(account.getBalance(), 108.18, 0.00000001); } @Test public void acctWithdrawal() { bankAccountClass account = new bankAccountClass("Fred","Flintstone",100.0); //try { account.withdrawal(50.00); //} //catch (acctOverdraftException e) {} Assert.assertEquals(account.getBalance(), 50.0, 0.00000001); } @Test public void acctWithdrawalMultiple() { bankAccountClass account = new bankAccountClass("Fred","Flintstone",45.45); //try { account.withdrawal(25.00); //} //catch (acctOverdraftException e) {} account.deposit(50.00); //try { account.withdrawal(10.25); //} //catch (acctOverdraftException e) {} account.deposit(12.73); Assert.assertEquals(account.getBalance(), 72.93, 0.00000001); } /* @Test //(expected = acctOverdraftException.class) public void acctWithdrawalOverdraft() { bankAccountClass account = new bankAccountClass("Fred","Flintstone",25.00); //try { account.withdrawal(50.00); //} //catch (acctOverdraftException e) {} } */ @Test public void acctEmailSentOnOverdraft() { EmailNotifier mockEmailNotifier = mock(EmailNotifier.class); bankAccountClass account = new bankAccountClass("Fred","Flintstone",25.00,mockEmailNotifier); account.withdrawal(50.00); verify(mockEmailNotifier).sendMessage("fred", "barney", "wilma", "betty"); Assert.assertEquals(account.getBalance(), 25.00, 0.0000001); } @Test public void acctDepositsFromDatabase() { daoBankAcctDatabase mockDB = mock(daoBankAcctDatabase.class); //List<Double> deposits = new ArrayList<Double>(); //deposits.add(11.12); //deposits.add(22.23); //deposits.add(33.34); double [] deposits = new double[3]; deposits[0] = 11.12; deposits[1] = 22.23; deposits[2] = 33.34; doReturn(deposits).when(mockDB).getDepositsFromDatabase(30); bankAccountClass account = new bankAccountClass("Fred","Flintstone",0.00,mockDB); //verify(mockDB.getDepositsFromDatabase(30)); Assert.assertEquals(account.sumDeposits(30),66.69,0.0000001); } } |