Grails 4 单元测试:"Invalid connection [ALL] configured for class..."

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

我在测试配置了多个数据源的 Grails 4 时遇到问题。

域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
    }
}

测试Class

class RoleSpec extends Specification implements DataTest {

    void setupSpec() {
        mockDomain Role
    }

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

我收到这个错误:


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.getDatastoreForQualifier(SimpleMapDatastore.java:256)
    at org.grails.datastore.mapping.simple.SimpleMapDatastore.getStaticApi(SimpleMapDatastore.java:226)
    at org.grails.datastore.gorm.GormEnhancer.registerEntity(GormEnhancer.groovy:154)
    at org.grails.datastore.mapping.simple.SimpleMapDatastore.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

我不知道该怎么做,所以我可以使用你能提供的任何帮助。

谢谢!

我明确列出了每个数据源,这解决了问题。

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

但我不太喜欢这个。我不明白为什么 ConnectionSource.ALL 不起作用,因为 docs 说它应该。