AccountService

The AccountService class is a core component designed to handle account transactions, specifically the transfer of funds between accounts. This class is part of a larger system that manages user accounts and their associated operations.

AccountService Class

Overview

The AccountService class is responsible for transferring funds from one account to another. It relies on an AccountManager interface to perform operations such as finding accounts and updating them after a transaction. The class is structured to allow for dependency injection of the AccountManager, which facilitates testing and flexibility.

Implementation

Below is the implementation of the AccountService class:

public class AccountService {   
    private AccountManager accountManager;

    public void setAccountManager(AccountManager manager) {
        this.accountManager = manager;
    }

    public void transfer(String senderId, String beneficiaryId, long amount) {
        Account sender = accountManager.findAccountForUser(senderId);
        Account beneficiary = accountManager.findAccountForUser(beneficiaryId);

        sender.debit(amount);
        beneficiary.credit(amount);
        this.accountManager.updateAccount(sender);
        this.accountManager.updateAccount(beneficiary);
    }
}

Key Methods

  • setAccountManager(AccountManager manager): This method allows the injection of an AccountManager implementation, which is crucial for the transfer method to function.

  • transfer(String senderId, String beneficiaryId, long amount): This method handles the transfer of a specified amount from the sender's account to the beneficiary's account. It performs the following steps:

    1. Retrieves the sender and beneficiary accounts using the AccountManager.
    2. Debits the sender's account and credits the beneficiary's account.
    3. Updates both accounts through the AccountManager.

Testing AccountService

To ensure the transfer method functions correctly, unit testing is performed using a mock implementation of the AccountManager. This approach allows testing the AccountService in isolation from the actual AccountManager implementation.

Testing with MockAccountManager

The following listing demonstrates how to test the transfer method using a mock:

public class TestAccountService {
   
    @Test  
    public void testTransferOk() {
        Account senderAccount = new Account("1", 200);                     
        Account beneficiaryAccount = new Account("2", 100);                

        MockAccountManager mockAccountManager = new MockAccountManager();  
        mockAccountManager.addAccount("1", senderAccount);                 
        mockAccountManager.addAccount("2", beneficiaryAccount);            

        AccountService accountService = new AccountService();              
        accountService.setAccountManager(mockAccountManager);              

        accountService.transfer("1", "2", 50);                             
        
        assertEquals(150, senderAccount.getBalance());                     
        assertEquals(150, beneficiaryAccount.getBalance());                
    }
}

Test Explanation

  • Setup: The test begins by creating two Account objects representing the sender and beneficiary. A MockAccountManager is then instantiated, and the accounts are added to it. This setup phase configures the mock to return the predefined accounts when queried.

  • Execution: The transfer method of AccountService is called to transfer 50 units from the sender to the beneficiary.

  • Verification: The test concludes by asserting that the balances of both accounts are as expected after the transfer, ensuring the method's correctness.

This testing approach highlights the importance of using mocks to isolate and verify the behavior of specific components within a system.