grails domain-class 验证器测试有问题

grails domain-class validator troubles testing

我在域 class 中有一个验证器,我在测试 Lagerort 控制器时遇到问题。

com.example.xyz.LagerortControllerSpec > Test the update action performs an update on a valid domain instance FAILED
java.lang.IllegalStateException: Either class [com.example.xyz.Lagertyp] is not a domain class or GORM has not been initialized correctly or has already been shutdown. Ensure GORM is loaded and configured correctly before calling any methods on a GORM entity.

如果我省略验证器,一切都测试正常,但这不是我想要的。

域名class:

 class Lagerort {

     String lagerort
     Lagertyp lagertyp
     String beschreibung

     static auditable = true

     static constraints = {
         lagerort(nullable: false, blank: false, unique: true)
         lagertyp(nullable: false, blank: false, validator: { val, obj ->
             // Only ONE Lagerort may be "Schrott"
             if (Lagertyp.count() > 0) {
                 def _LAGERTYPSTRING="Schrott"
                 Lagertyp lagertypschrott = Lagertyp.findByLagertyp(_LAGERTYPSTRING)
                 if (obj.lagertyp == lagertypschrott && Lagerort.countByLagertyp(lagertypschrott)>0) return ['lagerortschrottunique',_LAGERTYPSTRING]
             }
         })
         beschreibung(nullable: false, blank: false)

     }

     String toString(){lagerort}
 }

build.gradle 中依赖项的 testCompile 部分如下所示:

testCompile "org.grails:grails-plugin-testing"
testCompile "org.grails.plugins:geb"
testCompile "org.grails.plugins:hibernate5"
testRuntime "org.seleniumhq.selenium:selenium-htmlunit-driver:2.47.1"
testRuntime "net.sourceforge.htmlunit:htmlunit:2.18"

我已经尝试在控制器测试的设置部分创建一些 Lagertyp 类型的对象,以便 Lagertyp.count() > 0 对验证器来说是正确的,但这也无济于事。

LagerortControllerSpec/test 的 populateValidParams 如下所示:

def populateValidParams(params) {
    assert params != null
    params["lagerort"] = 'Fa.Conrad'
    params["lagertyp"] = ["lagertyp": 'Fa.Conrad', "beschreibung": 'Motor befindet sich bei Fa.Conrad']
    params["beschreibung"] = 'in Reparatur bei Fa. Conrad'
}

LagerortController:https://pastebin.com/PpZ5zqMm

LagerortController 测试:https://pastebin.com/pxZ6UeVK

有什么想法吗?

找到解决方案,我还必须模拟 Lagertyp,如下所示:

@Mock([Lagerort,Lagertyp])

看来我必须将所有域 类 包括在 @Mock 列表中的测试的一部分,即使是那些间接引用的域。