Search code examples
grailsgrails-ormgrails-4

Grails 4 Unit Test: "Invalid connection [ALL] configured for class..."


I'm having trouble testing Grails 4 with multiple datasources configured.

Domain Class

@GrailsCompileStatic
@EqualsAndHashCode(includes = 'authority')
@ToString(includes = 'authority', includeNames = true, includePackage = false)
class Role implements Serializable {

    String authority

    static constraints = {
        authority nullable: false, blank: false, unique: true
    }

    static mapping = {
        datasource ConnectionSource.ALL
    }
}

Test Class

class RoleSpec extends Specification implements DataTest {

    void setupSpec() {
        mockDomain Role
    }

    void "test nothing"() {
        expect:
        true
    }
}

I'm getting this error:


Invalid connection [ALL] configured for class [class us.cloudcard.api.Role]
org.grails.datastore.mapping.core.exceptions.ConfigurationException: Invalid connection [ALL] configured for class [class us.cloudcard.api.Role]
    at org.grails.datastore.mapping.simple.SimpleMapDatastore$4.getDatastoreForQualifier(SimpleMapDatastore.java:256)
    at org.grails.datastore.mapping.simple.SimpleMapDatastore$4.getStaticApi(SimpleMapDatastore.java:226)
    at org.grails.datastore.gorm.GormEnhancer.registerEntity(GormEnhancer.groovy:154)
    at org.grails.datastore.mapping.simple.SimpleMapDatastore$3.persistentEntityAdded(SimpleMapDatastore.java:218)
    at org.grails.datastore.mapping.model.AbstractMappingContext.addPersistentEntities(AbstractMappingContext.java:288)
    at grails.testing.gorm.DataTest$Trait$Helper.mockDomains(DataTest.groovy:80)
    at grails.testing.gorm.DataTest$Trait$Helper.mockDomain(DataTest.groovy:65)
    at grails.testing.gorm.DataTest$Trait$Helper.mockDomain(DataTest.groovy:64)
    at us.cloudcard.api.RoleSpec.setupSpec(RoleSpec.groovy:10)

application.yml

environments:
  development:
    dataSource:
      pooled: true
      driverClassName: com.mysql.jdbc.Driver
      (rest of the dev datasource ...)
  test:
    dataSource:
      dbCreate: update
      url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE
    dataSources:
      readReplica:
        dbCreate: update
        url: jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE

I have no idea where to go with this, so I could use whatever help you can give.

Thanks!


Solution

  • I listed each datasource explicitly, and that fixed the problem.

        static mapping = {
            cache true
            datasources([ConnectionSource.DEFAULT, 'readReplica'])
        }
    

    But I don't really like this. I don't understand why ConnectionSource.ALL doesn't work because the docs say that it should.

    enter image description here