我的dropwizard應用程序有一個集成測試,我使用hibernate h2數據庫來存儲數據。對於測試的第二部分,我使用jersey-testframework來處理資源和休息調用。集成測試的Dropwizard-Hibernate事務處理
爲了使這兩個部分一起工作,我必須爲每個案例創建一個TestRule。
休眠:
@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休眠DB 配置
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");
現在磨片N I呼籲資源請求 ...
@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]
顯然沒有事務處理的測試用例,但應該有一個。
我的普通dropwizard應用程序工作正常。這些交易通過使用@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的某個組件?