The Account Class Implementation


Here is the updated Account class implementation:

 package sis.studentinfo; import java.math.BigDecimal; import com.jimbob.ach.*; public class Account {    private BigDecimal balance = new BigDecimal("0.00");    private int transactionCount = 0;    private String bankAba;    private String bankAccountNumber;    private BankAccountType bankAccountType;    private Ach ach;    public enum BankAccountType { CHECKING("ck"), SAVINGS("sv");       private String value;       private BankAccountType(String value) {       this.value = value;       }       @Override       public String toString() {          return value;       }    }    public void credit(BigDecimal amount) {       balance = balance.add(amount);       transactionCount++;    }    public BigDecimal getBalance() {       return balance;    }    public BigDecimal transactionAverage() {       return balance.divide(          new BigDecimal(transactionCount), BigDecimal.ROUND_HALF_UP);    }    public void setBankAba(String bankAba) { this.bankAba = bankAba;    }    public void setBankAccountNumber(String bankAccountNumber) { this.bankAccountNumber = bankAccountNumber;    }    public void setBankAccountType(          Account.BankAccountType bankAccountType) {       this.bankAccountType = bankAccountType;    }    public void transferFromBank(BigDecimal amount) { AchCredentials credentials = createCredentials();       AchTransactionData data = createData(amount);       Ach ach = getAch(); AchResponse achResponse = ach.issueDebit(credentials, data);       credit(amount);    }    private AchCredentials createCredentials() { AchCredentials credentials = new AchCredentials(); credentials.merchantId = "12355";       credentials.userName = "sismerc1920";       credentials.password = "pitseleh411";       return credentials;    }    private AchTransactionData createData(BigDecimal amount) { AchTransactionData data = new AchTransactionData(); data.description = "transfer from bank";       data.amount = amount; data.aba = bankAba; data.account = bankAccountNumber; data.accountType = bankAccountType.toString(); return data;    }    private Ach getAch() { return ach;    }    void setAch(Ach ach) { this.ach = ach;    } } 

Account allows clients to pass it an Ach reference that it then stores. In the production system, your client code that constructs an Account will pass the Account a JimBobAch object. In the test, your code passes a MockAch object to the Account. In either case, Account doesn't know and doesn't care which concrete type it is sending the issueDebit message to.

While testTransferFromBank now passes, it is difficult to see exactly why. The mock class is in another source file, so to figure out what's going on, you must flip between the two class definitions. This isn't totally unacceptable, but you can improve upon things. One solution would be to include the mock definition as a nested class. Another would be to set up the mock directly within the test method, using an anonymous inner class.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net