我正在尝试使用 guice-persist,但我无法让它在一个简单的测试用例中工作。这是一个从调度程序调用的通用请求处理程序:
public abstract class TransactionAwareRequestHandler<T> extends SyncRequestHandler<T> {
@Transactional // Guice should make sure I get a transaction here
public final Object handleRequest(T request) {
return handleRequestWithTx(request);
}
public abstract Object handleRequestWithTx(T request);
}
我的测试处理程序是:
public static class TestHandler extends TransactionAwareRequestHandler {
@Override
public Object handleRequestWithTx(Object request) {
SessionFactory sessionFactory = injector.getProvider(SessionFactory.class).get();
assertTrue(sessionFactory.getCurrentSession().getTransaction().isActive()); // <<<--- Fails here!
return null;
}
}
Guice 设置:
injector = Guice.createInjector(
new HibernatePersistModule()
);
这将为所有带有注释的类和方法添加一个绑定拦截器@Transactional
测试用例:
@Test
public void shouldRunInTransaction() throws RequestHandlerException {
TestHandler handler = injector.getInstance(TestHandler.class);
// I'm not getting a proxy here! Why???
assertTrue("Expected proxy: " + handler.getClass(),
Proxy.isProxyClass(handler.getClass()));
handler.handleRequest(null);
}
问题:
为什么会这样?
我如何调试它以找出它发生的原因?有没有办法将日志记录添加到类/方法匹配器中?