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

时间:2011-05-22 04:01:36

标签: grails groovy integration

我正在浏览“Grails入门”电子书,并在第38页(实际第50页)第4章(验证)中遇到了问题。这是代码:

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

def code = badField?.codes.find { 
   it == 'race.startDate.validator.invalid'
}

正如我所说,它不会影响主要的执行,但只是好奇我是不对的,或者如果这是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
    }
}

其中,当运行“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

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

任何人都可以解释一下我能做些什么来解决这个问题吗?

5 个答案:

答案 0 :(得分:5)

我刚收到此错误,原因是我的测试类错误。

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

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

答案 1 :(得分:3)

我遇到了同样的问题(虽然我使用的是Grails 2.3.4) - 我明确地修改了它

import racetrack.Race

而不是

package racetrack

有趣的是,在我尝试这个之后,我评论了它,一切仍然有效 - 直到我做了一个grails清理。然后又失败了。在grails / groovy自动编译中怀疑不是100%的东西。

答案 2 :(得分:2)

我用Grails 2.4.2解决了这个问题。原因是我有一个名为FooTest的测试文件,但该类被命名为FooTest ** s **。

正在运行grails test-app --stacktrace有助于找到有问题的课程。

答案 3 :(得分:0)

首先,我认为您不需要将其作为'整合'测试。将它放在'src / test / unit / ...'目录结构下。其次,如果你想测试Grails的validate()'方法,它将由Grails框架基于你的'constraints'块注入,你必须使测试扩展'GrailsUnitTest'并调用'mockDomain(Race) )'在你的单元测试方法的第一行。如果不清楚,请打电话给我,我会发布代码,但我的猜测是你的书有一个非常好的例子。这是一些可以修复它的“自由手”代码......

class RaceTests extends GrailsUnitTest {

    void testRaceDatesBeforeToday() {
        mockDomain(Race)
        ...

答案 4 :(得分:0)

请确保您的软件包名称正确,上述错误意味着它尝试运行测试,但由于软件包名称指定错误,因此无法找到具有该特定软件包名称的文件。