MockControl sessionControl = MockControl.createControl(Session.class);
Session session = (Session) sessionControl.getMock();
OpenSessionInViewInterceptor rawInterceptor = new OpenSessionInViewInterceptor();
rawInterceptor.setSessionFactory(sf);
HandlerInterceptor interceptor = new WebRequestHandlerInterceptorAdapter(rawInterceptor);
MockServletContext sc = new MockServletContext();
MockHttpServletRequest request = new MockHttpServletRequest(sc);
MockHttpServletResponse response = new MockHttpServletResponse();
sf.openSession();
sfControl.setReturnValue(session, 1);
session.getSessionFactory();
sessionControl.setReturnValue(sf, 2);
session.isOpen();
sessionControl.setReturnValue(true, 1);
session.setFlushMode(FlushMode.NEVER);
sessionControl.setVoidCallable(1);
sfControl.replay();
sessionControl.replay();
interceptor.preHandle(request, response, "handler");
assertTrue(TransactionSynchronizationManager.hasResource(sf));
// check that further invocations simply participate
interceptor.preHandle(request, response, "handler");
assertEquals(session, SessionFactoryUtils.getSession(sf, false));
interceptor.preHandle(request, response, "handler");
interceptor.postHandle(request, response, "handler", null);
interceptor.afterCompletion(request, response, "handler", null);
interceptor.postHandle(request, response, "handler", null);
interceptor.afterCompletion(request, response, "handler", null);
interceptor.preHandle(request, response, "handler");
interceptor.postHandle(request, response, "handler", null);
interceptor.afterCompletion(request, response, "handler", null);
sfControl.verify();
sessionControl.verify();
sfControl.reset();
sessionControl.reset();
sfControl.replay();
sessionControl.replay();
interceptor.postHandle(request, response, "handler", null);
assertTrue(TransactionSynchronizationManager.hasResource(sf));
sfControl.verify();
sessionControl.verify();
sfControl.reset();
sessionControl.reset();
session.close();
sessionControl.setReturnValue(null, 1);
sfControl.replay();
sessionControl.replay();
interceptor.afterCompletion(request, response, "handler", null);
assertFalse(TransactionSynchronizationManager.hasResource(sf));
sfControl.verify();
sessionControl.verify();
}