我的DropWizard应用程序有一个集成测试,我使用Hibernate H2数据库来存储数据。对于测试的第二部分,我使用jersey-testframework来处理资源和休息调用。
为了使这两个部分协同工作,我必须为每个案例创建一个测试规则。
冬眠:
@ClassRule
public static final SessionFactoryRule sessionFactoryRule = new SessionFactoryRule(User.class, HibernateUtil.getHibernateClasses());
public static final ProgramDAO programDao = new ProgramDAO(sessionFactoryRule.getSessionFactory());
泽西:
@ClassRule
public static final ResourceTestRule resources = ResourceTestRule.builder()
.setTestContainerFactory(new GrizzlyWebTestContainerFactory())
.addProvider(new AuthDynamicFeature(
new OAuthCredentialAuthFilter.Builder<User>()
.setAuthenticator(new OAuthAuthenticator(userManagement))
.setAuthorizer(new OAuthAuthorizer())
.setPrefix(TOKEN_TYPE)
.buildAuthFilter()))
.addProvider(new AuthValueFactoryProvider.Binder<>(User.class))
.addProvider(RolesAllowedDynamicFeature.class)
.addResource(new AdResource(urlMapperMock, programDao))
.build();
h2 休眠数据库配置
Configuration configuration = new Configuration()
.setInterceptor(new SoftDeleteInterceptor())
.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect")
.setProperty("hibernate.connection.driver_class", "org.h2.Driver")
.setProperty("hibernate.connection.url", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1")
.setProperty("hibernate.hbm2ddl.auto", "create")
.setProperty("hibernate.default_batch_fetch_size", "200")
.setProperty("hibernate.show_sql", "false")
.setProperty("hibernate.current_session_context_class", "thread");
现在,当我调用资源上的请求时...
@Test
public void resourceCheck() {
javax.ws.rs.core.Response response = resources.getJerseyTest().target(Ad.TABLE_NAME).request().header(HEADER_KEY_AUTHORIZATION, HEADER_VALUE_VALID_BEARER_TOKEN).get();
assertThat(response.getStatus()).isEqualTo(200);
}
。引发以下错误
ERROR [2016-01-26 11:23:21,594] org.glassfish.grizzly.servlet.ServletHandler: service exception:
! org.hibernate.HibernateException: getNamedQuery is not valid without active transaction
! at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
! at com.sun.proxy.$Proxy74.getNamedQuery(Unknown Source) ~[na:na]
! at io.dropwizard.hibernate.AbstractDAO.namedQuery(AbstractDAO.java:57) ~[dropwizard-hibernate-0.9.1.jar:0.9.1]
! at com.ad4mat.api.db.core.AdDAO.findAllByUser(AdDAO.java:36) ~[classes/:na]
! at com.ad4mat.api.resource.AdResource.getAll(AdResource.java:79) ~[classes/:na]
显然,测试用例没有事务处理,但应该有一个。
我的正常向导应用程序工作正常。事务是通过用 @UnitOfWork
标记资源来处理的。
当我在道中添加曼努埃尔交易时,例如...
public List<Ad> findAllByUser(User user) {
currentSession().beginTransaction();
Query query = namedQuery("ad.findAllByUser")
.setParameter(User.FOREIGN_COLUMN_ID, user.getId());
return query.list();
集成测试有效,但我的应用程序将引发嵌套事务异常。
所以我的问题是:
如何使@UnitOfWork注释可用于集成测试过程。显然缺少事务处理实现。我必须jersey.register()
Dropwizard-Hibernate的某个组件吗?
从dropwizard版本0.9.2开始,可以在泽西岛上下文之外使用@UnitOfWork注释。也许您正在寻找一个向导更新;然后,您应该能够在任何上下文中执行数据库工作(最重要的是从Junit/testng上下文中)。http://www.dropwizard.io/0.9.2/docs/about/release-notes.html