Adapters


Overusing anonymous inner classes can actually make code harder to follow. Your goal should be to keep methods short, even test methods. The long anonymous inner class implementation in testTransferFromBank means that you will probably need to scroll in order to get the big picture of what the method is doing.

You can create an interface adapter class that provides an empty implementation for the Ach interface. Modify the MockAch class to supply an empty definition for the issueDebit method.

 package sis.studentinfo; import java.util.*; import com.jimbob.ach.*; import junit.framework.Assert; class MockAch implements Ach {    public AchResponse issueDebit(          AchCredentials credentials, AchTransactionData data) {       return null;    }    public AchResponse markTransactionAsNSF(AchCredentials credentials,          AchTransactionData data,          String traceCode) {       return null;    }    public AchResponse refundTransaction(AchCredentials credentials,          AchTransactionData data,          String traceCode) {       return null;    }    public AchResponse issueCredit(AchCredentials credentials,          AchTransactionData data) {       return null;    }    public AchResponse voidSameDayTransaction(          AchCredentials credentials,          AchTransactionData data,          String traceCode) {       return null;    }    public AchResponse queryTransactionStatus(AchCredentials credentials,          AchTransactionData data, String traceCode) {       return null;    } } 

Back in AccountTest, change the instantiation from new Ach() to new MockAch(). Remove the method implementations for everything but issueDebit.

 public void testTransferFromBank() {    Ach mockAch = new MockAch() {       public AchResponse issueDebit(             AchCredentials credentials, AchTransactionData data) {          Assert.assertTrue(             data.account.equals(AccountTest.ACCOUNT_NUMBER));          Assert.assertTrue(data.aba.equals(AccountTest.ABA));          AchResponse response = new AchResponse();          response.timestamp = new Date();          response.traceCode = "1";          response.status = AchStatus.SUCCESS;          return response;       }    };    account.setAch(mockAch);    final BigDecimal amount = new BigDecimal("50.00");    account.transferFromBank(amount);    assertEquals(amount, account.getBalance()); } 

You are still creating an anonymous inner class. An anonymous inner class can either implement an interface or subclass another class. This code demonstrates the latter. Here, you use the MockAch class as an adapter to hide the methods that your mock doesn't need to worry about. Now you can include the relevant mock code in a reasonably brief test method.



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