2013-08-06 90 views
3

我正在使用帶有嵌入式Glassfish 4.0容器的Arquillian進行測試。到目前爲止,我設法讓它起作用,但是一個測試案例一直失敗,我不知道爲什麼。這裏是我的測試代碼:JavaEE CDI測試:獲取RollbackException的原因

// 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.eclip[email protected]fe04c00, [email protected]e9d9c24, [email protected]d7627ce, com.sun.e[email protected]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

你沒有得到任何堆棧跟蹤或控制檯中的任何東西或作爲返回?我也猜測你的異常是在保存方法之後發生的? – LightGuard

+0

嗨,我更新了問題將我輸出的結果。此外,由於保存方法發生異常,因此不會執行任何語句。 – bmurauer

回答

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

相關問題