hivemind-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "James Carman" <ja...@carmanconsulting.com>
Subject MethodInterceptorFactory Documentation...
Date Sat, 26 Mar 2005 15:56:26 GMT
All,

Do you think we should we include a little snippet about how using
MethodInterceptors allows us to easily unit test our interceptor logic?

public void testWithTransaction() throws Throwable
{
  // Set up mock objects...
  MockManager mocks = new MockManager();
  TransactionInterceptor interceptor = new TransactionInterceptor();
  MockControl transactionServiceControl = mocks.createControl(
TransactionService.class );
  TransactionService mockTransactionService = ( TransactionService )
transactionServiceControl.getMock();
  MockControl invocationControl = mocks.createControl(
MethodInvocation.class );
  MethodInvocation mockInvocation = ( MethodInvocation
)invocationControl.getMock();
  
  // Train mock objects...
  mockTransactionService.isActive();
  transactionServiceControl.setReturnValue( true );
  mockInvocation.proceed();
  invocationControl.setReturnValue( null );
  mocks.replayAll();
  
  // Inject dependencies...
  interceptor.setTransactionService( mockTransactionService );
  
  // Run tests...
  interceptor.invoke( mockInvocation );

  // Verify mock objects...
  mocks.verifyAll();
}

The MockManager, here, does what you might expect.  It just keeps track of
all my mock objects and allows me to call replay() and verify() on all of
them as a whole.

James



---------------------------------------------------------------------
To unsubscribe, e-mail: hivemind-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: hivemind-dev-help@jakarta.apache.org


Mime
View raw message