3

我正在使用带有嵌入式 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),但我现在排除了这一点。

我没有得到完整的堆栈跟踪,只有测试运行结束时的错误。这是输出:

[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
------------------------------------------------------------------------
4

1 回答 1

2

这不是 Arquillian 的问题。

由于您在 StackTrace 中找不到异常,我想原始异常被包装到 RollbackException 中。根据 JTA 1.2 规范,任何未经检查的异常都会自动回滚事务。

您能否确认在您的任何底层方法中都没有抛出未经检查的异常(例如 RuntimeException、Nullpointer 等) - 甚至可能来自某些自定义验证器。这是供进一步参考的 JTA 1.2 规范:https ://java.net/projects/jta-spec/sources/spec-source-repository/content/jta-1_2-spec_v2.pdf?rev=14

于 2013-08-07T09:09:13.437 回答