You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hivemind.apache.org by James Carman <ja...@carmanconsulting.com> on 2005/03/26 16:56:26 UTC

MethodInterceptorFactory Documentation...

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