No transactionManager error in Grails 3 Integration test
Asked Answered
O

2

7

I have created a new Grails 3.1.4 angular project along with a few domain objects and controllers extending RestfulController. I have created the Integration test below. When I run grails test-app -integration I get the error

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)

Test class:

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

I do have a datasource set up in 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
Obstinate answered 30/3, 2016 at 12:10 Comment(3)
The only reason I want an integration test is that I have a custom JSON Marshaller that is not invoked under a unit test.Obstinate
Have you ever figured this out? I'm migrating a Grails 2 application and seeing the same issue with some of our integration tests.Ardatharde
@rado I just removed the custom marshalers in favor of gson. This is the Grails 3 way to go :-)Obstinate
W
3

This can help if you do not have a persistence plugin configured in your build.gradle that sets up a transaction manager (examples include hibernate4, mongodb, neo4j etc. or you do not have a dataSource configured in grails-app/conf/application.yml.

If this is the case, simply remove the @Rollback annotation and that should fix the problem.

Wellappointed answered 31/3, 2016 at 7:20 Comment(1)
I would like to keep the @Rollback so that the test run does not pollute the database for other tests. I did try to remove the rollback, but got this error on running the test: org.springframework.dao.DataAccessResourceFailureException: Could not obtain current Hibernate Session; nested exception is org.hibernate.HibernateException: No Session found for current threadObstinate
C
0

Ran across this while troubleshooting my own integration test. I resolved my problem by deleting the out directory.

delete project-folder/out/

Now you'll also need to clean and rebuild a war file. This must run some extra steps in the build process and resolves some issues

./grailsw clean
./grailsw war

Now when you run your tests you shouldn't see the error message.

Curet answered 29/6, 2018 at 18:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.