开发者

Getting compile error in integration testing with "Getting Started with Grails" ebook

I'm going through the "Getting Started with Grails" ebook and have hit a wall with chapter 4 (Validation) on page 38 (actual page 50). Here is the code:

Oh, there might be a typo in the code in the book, though it didn't affect the behavior or error messages I got, on the following line:

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

As I said, it doesn't affect the main execution, but was just curious if I'm right or if this is something in Groovy I haven't run across yet. I put what I thought it should be below.

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

where, when running "grails test-app", I get the following:

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

The book is using Grails 1.2.x and I'm using 1.3.x and already noticed some discrepancies between the versions (nothing unsurmountable), so it could be something like that, but I can't seem to figure it out. Being new to Groovy and Grails isn't helping! :-D

Can anyone explain what I can do to get past this?


I just got this error, my cause was that my test class was in the wrong package.

I could not find a way to get a clearer description of the problem, even running with the --stacktrace option showed no more information.

It seems like this error can be caused by different compilation issues, perhaps?


I had the same problem (although I'm using Grails 2.3.4) - I fixed it by explicitly including

import racetrack.Race

instead of

package racetrack

Interestingly, after I tried this I commented it out and everything still worked - until I did a grails clean. Then it failed again. Suspect something not quite 100% in the grails / groovy auto compilation stuff.


I hit this problem with Grails 2.4.2. The cause was I had a test file named FooTest, but the class was named FooTest**s**.

Running grails test-app --stacktrace helped find the offending class.


First of all, I don't think you need this to be an 'integration' test. Place it under the 'src/test/unit/...' directory structure. Second of all, if you want to test the Grails 'validate()' method that is going to be injected by the Grails framework based on your 'constraints' block, you must make the test extend 'GrailsUnitTest' and call 'mockDomain(Race)' on the first line of your unit test method. If that is unclear, ping me and I'll post code but my guess is your book has a pretty good example of this. Here is some 'free hand' code that might fix it...

class RaceTests extends GrailsUnitTest {

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


please make sure that your package name is correct, the above error means that its trying to run the test but since the package name is specified wrong its not able to find the file with that particular package name.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜