2016-03-30 9 views
7

Ich habe ein neues Grails 3.1.4 eckiges Projekt zusammen mit einigen Domänenobjekten und Controllern erstellt, die RestfulController erweitern. Ich habe den Integrationstest unten erstellt. Als ich grails test-app -integration laufen erhalte ich die FehlerKein transactionManager Fehler in Grails 3 Integrationstest

java.lang.IllegalStateException: No transactionManager was specified. Using @Transactional or @Rollback requires a valid configured transaction manager. If you are running in a unit test ensure the test has been properly configured and that you run the test suite not an individual test method. 
    at grails.transaction.GrailsTransactionTemplate.<init>(GrailsTransactionTemplate.groovy:60) 
    at com.waldoware.invoicer.BillingEntityRestControllerIntegrationSpec.$tt__$spock_feature_0_0(BillingEntityRestControllerIntegrationSpec.groovy:29) 
    at com.waldoware.invoicer.BillingEntityRestControllerIntegrationSpec.test all entities_closure2(BillingEntityRestControllerIntegrationSpec.groovy) 
    at groovy.lang.Closure.call(Closure.java:426) 
    at groovy.lang.Closure.call(Closure.java:442) 
    at grails.transaction.GrailsTransactionTemplate$1.doInTransaction(GrailsTransactionTemplate.groovy:70) 
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133) 
    at grails.transaction.GrailsTransactionTemplate.executeAndRollback(GrailsTransactionTemplate.groovy:67) 
    at com.waldoware.invoicer.BillingEntityRestControllerIntegrationSpec.test all entities(BillingEntityRestControllerIntegrationSpec.groovy) 

Prüfklasse:

package com.waldoware.invoicer 

import grails.test.mixin.integration.Integration 
import grails.transaction.* 
import spock.lang.* 

@Integration 
@Rollback 
class BillingEntityRestControllerIntegrationSpec extends Specification { 

    def setupData() { 
     def biller = new BillingEntity() 
     biller.with { 
      companyName = "Acme, Inc." 
     } 
     def ledger = new Ledger(name: "My Ledger", billingEntity: biller).save(failOnError: true, flush: true) 
    } 

    void 'test all entities'() { 
     when: 
     setupData() 
     new BillingEntityRestController().index() 

     then: 
     response.contentType == 'application/json;charset=UTF-8' 
     response.status == HttpServletResponse.SC_OK 
     response.text == "[{}]" 
    } 
} 

Ich habe eine Datenquelle in application.yml einzurichten:

environments: 
    development: 
     dataSource: 
      dbCreate: none 
      url: jdbc:h2:./devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE 
    test: 
     dataSource: 
      dbCreate: update 
      url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE 
    production: 
     dataSource: 
      dbCreate: update 
      url: jdbc:h2:./prodDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE 
      properties: 
       jmxEnabled: true 
       initialSize: 5 
       maxActive: 50 
       minIdle: 5 
       maxIdle: 25 
       maxWait: 10000 
       maxAge: 600000 
       timeBetweenEvictionRunsMillis: 5000 
       minEvictableIdleTimeMillis: 60000 
       validationQuery: SELECT 1 
       validationQueryTimeout: 3 
       validationInterval: 15000 
       testOnBorrow: true 
       testWhileIdle: true 
       testOnReturn: false 
       jdbcInterceptors: ConnectionState 
       defaultTransactionIsolation: 2 # TRANSACTION_READ_COMMITTED 
+1

Der einzige Grund für einen Integrationstest ist, dass ich einen benutzerdefinierten JSON Marshaller habe, der nicht unter einem Komponententest aufgerufen wird. –

+0

Haben Sie das jemals herausgefunden? Ich migriere eine Grails-2-Anwendung und sehe das gleiche Problem mit einigen unserer Integrationstests. – Rado

+0

@rado Ich habe nur die benutzerdefinierten Marshaller zugunsten von Gson entfernt. Das ist der Grails 3 Weg :-) –

Antwort

3

Dies kann helfen, wenn Sie nicht haben, ein Persistenz-Plugin, das in Ihrem build.gradle konfiguriert ist, das einen Transaktionsmanager einrichtet (Beispiele sind hibernate4, mongodb, neo4j usw. oder Sie haben keine dataSource konfiguriert in grails-app/conf/application.yml.

Wenn dies der Fall ist, entfernen Sie einfach die @Rollback Annotation und das sollte das Problem beheben.

+3

Ich möchte den 'Rollback' beibehalten, damit der Testlauf die Datenbank nicht für andere Tests verschmutzt. Ich habe versucht, das Rollback zu entfernen, aber beim Ausführen des Tests ist folgender Fehler aufgetreten: 'org.springframework.dao.DataAccessResourceFailureException: Die aktuelle Hibernate-Sitzung konnte nicht abgerufen werden. Verschachtelte Ausnahme ist org.hibernate.HibernateException: Keine Sitzung für den aktuellen Thread gefunden –