FileDocCategorySizeDatePackage
MockInterceptorTest.javaAPI DocExample1984Mon Apr 25 08:20:18 BST 2005None

MockInterceptorTest

public class MockInterceptorTest extends TestCase implements com.springbook.InterceptMe

Fields Summary
Constructors Summary
Methods Summary
public voidinterceptThisMethod()

      // empty method
   
public voidtestLoggingInterceptor()

      MockControl factoryControl = MockControl.createControl(SessionFactory.class);
      MockControl sessionControl = MockControl.createControl(Session.class);

      SessionFactory mockFactory =
            (SessionFactory)factoryControl.getMock();
      Session mockSession = (Session)sessionControl.getMock();
      // this sequence will execute once before the target method...
      mockFactory.openSession();
      factoryControl.setReturnValue(mockSession);
      mockSession.save(new LogEvent());
      sessionControl.setMatcher(MockControl.ALWAYS_MATCHER);
      sessionControl.setReturnValue(null);
      mockSession.close();
      sessionControl.setReturnValue(null);
      // and once after
      mockFactory.openSession();
      factoryControl.setReturnValue(mockSession);
      mockSession.save(new LogEvent());
//      sessionControl.setMatcher(MockControl.ALWAYS_MATCHER);
      sessionControl.setReturnValue(null);
      mockSession.close();
      sessionControl.setReturnValue(null);
      factoryControl.replay();
      sessionControl.replay();


      Advice advice = new LoggingAround();
      ((LoggingAround)advice).setFactory(mockFactory);
      ProxyFactory proxyFactory = new ProxyFactory(this);
      proxyFactory.addAdvice(advice);
      InterceptMe target = (InterceptMe)proxyFactory.getProxy();
      target.interceptThisMethod();

      factoryControl.verify();
      sessionControl.verify();