2016-03-30 33 views
7

Ho creato un nuovo progetto angolare di Grails 3.1.4 insieme ad alcuni oggetti dominio e controller che si estendono RestfulController. Ho creato il test di integrazione di seguito. Quando eseguo grails test-app -integration ottengo l'erroreErrore transactionManager in Grails 3 Test di integrazione

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) 

classe Test:

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 == "[{}]" 
    } 
} 

ho un DataSource istituito nel application.yml:

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

L'unico motivo per cui desidero un test di integrazione è che dispongo di un Marshaller JSON personalizzato non richiamato in un test di unità. –

+0

Hai mai capito questo? Sto migrando un'applicazione Grails 2 e ho riscontrato lo stesso problema con alcuni dei nostri test di integrazione. – Rado

+0

@rado Ho appena rimosso i marshaler personalizzati in favore di Gson. Questa è la via per Grails 3 :-) –

risposta

3

Questo può aiutare se non si dispone di un plug-in di persistenza configurato nel tuo build.gradle che configura un gestore transazioni (esempi includono hibernate4, mongodb, neo4j ecc. O non si dispone di un dataSource configurato in grails-app/conf/application.yml.

Se questo è il caso, è sufficiente rimuovere l'annotazione @Rollback e questo dovrebbe risolvere il problema.

+3

Vorrei mantenere il '@ Rollback' in modo che l'esecuzione di test non inquini il database per altri test. Ho provato a rimuovere il rollback, ma ho ricevuto questo errore durante l'esecuzione del test: 'org.springframework.dao.DataAccessResourceFailureException: Impossibile ottenere la sessione di ibernazione corrente; l'eccezione annidata è org.hibernate.HibernateException: Nessuna sessione trovata per il thread corrente' –