我正在使用带有嵌入式Glassfish 4.0容器的Arquillian进行测试。到目前为止,我设法让它工作,但一个测试用例仍然失败,我不知道为什么。这是我的测试代码:
// Test Class
@Inject
private CompetenceService competenceService;
@Test
public void createSingleCompetence() {
Competence c = new Competence();
// fill the competence with data
c = competenceService.save(c); // throws a RollbackException
// some testing assertions
}
这是服务类:
// CompetenceService
@Transactional
@ApplicationScoped
public class CompetenceService {
@PersistenceContext
private EntityManager em;
public Competence save(Competence c){
return em.merge(c);
}
}
使用arquillian运行时,会出现以下错误:
Managed bean with Transactional annotation and TxType of REQUIRED encountered exception during commit javax.transaction.RollbackException: Transaction marked for rollback
我的问题是:我能以某种方式弄清楚出了什么问题吗?我的第一个想法是并非所有约束都得到满足(如@NotNull
),但我现在已经排除了这一点。
我没有得到完整的堆栈跟踪,只有testrun结束时的错误。这是输出:
[Competence test competence]
Aug 07, 2013 8:28:57 AM org.glassfish.cdi.transaction.TransactionalInterceptorRequired transactional
INFO: In REQUIRED TransactionalInterceptor
Aug 07, 2013 8:28:57 AM org.glassfish.cdi.transaction.TransactionalInterceptorRequired transactional
INFO: Managed bean with Transactional annotation and TxType of REQUIRED called outside a transaction context. Beginning a transaction...
Aug 07, 2013 8:28:57 AM org.glassfish.cdi.transaction.TransactionalInterceptorBase markRollbackIfActiveTransaction
INFO: About to setRollbackOnly from @Transactional interceptor on transaction:JavaEETransactionImpl: txId=1 nonXAResource=1 jtsTx=null localTxStatus=0 syncs=[org.eclipse.persistence.internal.jpa.transaction.JTATransactionWrapper$1@fe04c00, org.eclipse.persistence.transaction.JTASynchronizationListener@4e9d9c24, org.eclipse.persistence.transaction.JTASynchronizationListener@4d7627ce, com.sun.enterprise.resource.pool.PoolManagerImpl$SynchronizationListener@82f6d1d]
Aug 07, 2013 8:28:57 AM org.glassfish.cdi.transaction.TransactionalInterceptorRequired transactional
INFO: Managed bean with Transactional annotation and TxType of REQUIRED encountered exception during commit javax.transaction.RollbackException: Transaction marked for rollback.
Aug 07, 2013 8:28:58 AM org.glassfish.persistence.common.Java2DBProcessorHelper executeDDLs
WARNING: PER01000: Got SQLException executing statement "ALTER TABLE COMPETENCECATEGORY DROP CONSTRAINT CMPTWNNGNSTNCNTTYD": java.sql.SQLSyntaxErrorException: ALTER TABLE failed. There is no constraint 'APP.CMPTWNNGNSTNCNTTYD' on table '"APP"."COMPETENCECATEGORY"'.
PlainTextActionReporterSUCCESS
PER01003: Deployment encountered SQL Exceptions:
PER01000: Got SQLException executing statement "ALTER TABLE COMPETENCECATEGORY DROP CONSTRAINT CMPTWNNGNSTNCNTTYD": java.sql.SQLSyntaxErrorException: ALTER TABLE failed. There is no constraint 'APP.CMPTWNNGNSTNCNTTYD' on table '"APP"."COMPETENCECATEGORY"'. Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 23.032 sec <<< FAILURE!
Aug 07, 2013 8:29:01 AM org.glassfish.admin.mbeanserver.JMXStartupService shutdown
INFO: JMXStartupService and JMXConnectors have been shut down.
JdbcRuntimeExtension, getAllSystemRAResourcesAndPools = [GlassFishConfigBean.org.glassfish.jdbc.config.JdbcResource, GlassFishConfigBean.org.glassfish.jdbc.config.JdbcResource, GlassFishConfigBean.org.glassfish.jdbc.config.JdbcConnectionPool, GlassFishConfigBean.org.glassfish.jdbc.config.JdbcConnectionPool, GlassFishConfigBean.org.glassfish.jdbc.config.JdbcConnectionPool, GlassFishConfigBean.org.glassfish.jdbc.config.JdbcResource]
Aug 07, 2013 8:29:01 AM com.sun.enterprise.connectors.service.ResourceAdapterAdminServiceImpl sendStopToResourceAdapter
INFO: RAR7094: __ds_jdbc_ra shutdown successful.
Aug 07, 2013 8:29:01 AM com.sun.enterprise.v3.server.AppServerStartup stop
INFO: Shutdown procedure finished
Results :
Tests in error:
createSingleCompetence(at.seresunit.outtasking.test.CompetenceTest): Managed bean with Transactional annotation and TxType of REQUIRED encountered exception during commit javax.transaction.RollbackException: Transaction marked for rollback.
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
答案 0 :(得分:2)
这对Arquillian来说不是问题。
由于您在StackTrace中找不到异常,我认为原始的Exception被包装到RollbackException中。根据JTA 1.2规范,任何未经检查的Exception都将自动回滚事务。
您能否确认在任何底层方法中都没有抛出未经检查的异常(例如RuntimeException,Nullpointer等) - 甚至可能来自某个自定义验证器。 这是JTA 1.2规范以供进一步参考:https://java.net/projects/jta-spec/sources/spec-source-repository/content/jta-1_2-spec_v2.pdf?rev=14