Monday, December 27, 2004

 

Hello

import static org.easymock.EasyMock.*;

import org.easymock.IMocksControl;

import junit.framework.TestCase;

public class TestAccountTransfer extends TestCase
{
  private IMocksControl control;
  private IAccount account1;
  private IAccount account2;

  protected void setUp() throws Exception
  {
    super.setUp();
    control = createStrictControl();
    account1 = control.createMock(IAccount.class);
    account2 = control.createMock(IAccount.class);
  }

  protected void tearDown() throws Exception
  {
    super.tearDown();
    control.verify();
  }
  
  public void test_can_transfer_from_one_account_to_another() throws Exception
  {
    // Record expectation in mock objects
    account1.debit(599);
    account2.credit(599);
    control.replay();
    
    // Run through the tested sequence and verify that it meets the expectation
    AccountUtils.transfer(account1, account2, 599);
  }
  
  public void test_transfer_is_cancelled_if_source_has_insufficient_balance() throws Exception
  {
    account1.debit(599);
    expectLastCall().andThrow(new Exception("Insufficient balance"));
    control.replay();
    
    try
    {
      AccountUtils.transfer(account1, account2, 599);
      fail("Did not throw exception on unsuccessful debit");
    }
    catch (Exception e) {}
  }
}

Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?