使用"Grails入门"电子书在集成测试中获得编译错误

mik*_*ard 3 integration grails groovy

我正在阅读"Grails入门"电子书,并在第38页(实际第50页)第4章(验证)中遇到了问题.这是代码:

哦,书中的代码可能有拼写错误,但它不会影响我得到的行为或错误消息,如下所示:

def code = badField?.codes.find { 
   it == 'race.startDate.validator.invalid'
}
Run Code Online (Sandbox Code Playgroud)

正如我所说,它不会影响主要的执行,但只是好奇,如果我是对的,或者如果这是Groovy中的东西,我还没有遇到过.我把我认为它应该放在下面.

package racetrack

import groovy.util.GroovyTestCase


class RaceIntegrationTests extends GroovyTestCase { 

    void testRaceDatesBeforeToday() {
    def lastWeek = new Date() - 7 
    def race = new Race(startDate:lastWeek) 
    assertFalse "Validation should not succeed", race.validate() 
    // It should have errors after validation fails 
    assertTrue "There should be errors", race.hasErrors()

    println "\nErrors:" 
    println race.errors ?: "no errors found"

    def badField = race.errors.getFieldError('startDate') 
    println "\nBadField:" 
    println badField ?: "startDate wasn't a bad field" 
    assertNotNull "Expecting to find an error on the startDate field", badField

    def code = badField ?: codes.find { 
        it == 'race.startDate.validator.invalid'
    } 
    println "\nCode:" 
    println code ?:"the custom validator for startDate wasn't found" 
    assertNotNull "startDate field should be the culprit", code
    }
}
Run Code Online (Sandbox Code Playgroud)

在哪里,当运行"grails test-app"时,我得到以下内容:

Error executing script TestApp: java.lang.RuntimeException: Could not load class in test type 'integration'
java.lang.RuntimeException: Could not load class in test type 'integration'
at gant.Gant$_dispatch_closure5.doCall(Gant.groovy:391)
at gant.Gant$_dispatch_closure7.doCall(Gant.groovy:415)
at gant.Gant$_dispatch_closure7.doCall(Gant.groovy)
at gant.Gant.withBuildListeners(Gant.groovy:427)
at gant.Gant.this$2$withBuildListeners(Gant.groovy)
at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source)
at gant.Gant.dispatch(Gant.groovy:415)
at gant.Gant.this$2$dispatch(Gant.groovy)
at gant.Gant.invokeMethod(Gant.groovy)
at gant.Gant.executeTargets(Gant.groovy:590)
at gant.Gant.executeTargets(Gant.groovy:589)
Caused by: java.lang.RuntimeException: Could not load class in test type 'integration'
at _GrailsTest_groovy$_run_closure4.doCall(_GrailsTest_groovy:261)
at _GrailsTest_groovy$_run_closure4.call(_GrailsTest_groovy)
at _GrailsTest_groovy$_run_closure2.doCall(_GrailsTest_groovy:228)
at _GrailsTest_groovy$_run_closure1_closure21.doCall(_GrailsTest_groovy:187)
at _GrailsTest_groovy$_run_closure1.doCall(_GrailsTest_groovy:174)
at TestApp$_run_closure1.doCall(TestApp.groovy:82)
at gant.Gant$_dispatch_closure5.doCall(Gant.groovy:381)
... 10 more
Run Code Online (Sandbox Code Playgroud)

这本书使用的是Grails 1.2.x,我使用的是1.3.x,并且已经注意到版本之间存在一些差异(没有什么不可克服的),所以它可能就是这样,但我似乎无法弄明白.不熟悉Groovy和Grails并没有帮助!:-D

任何人都可以解释我能做些什么才能超越这个?

syn*_*ync 5

我刚收到这个错误,我的原因是我的测试类在错误的包中.

我找不到一种方法来更清楚地描述问题,即使运行--stacktrace选项显示没有更多的信息.

似乎这个错误可能是由不同的编译问题引起的,也许?