简体   繁体   中英

Grails: How to mock a domain field validator?

Is there some way to mock a domain field validator?
Currently, my code in the domain class is looking like this:

isPrimary(validator: { Boolean value, Person obj ->
     .......
}

And I need to mock this function.

I tried to use it like:

Person.metaClass.static.isPrimary.validator = { Boolean value, Person obj ->
     .......
}

And it didn't work, any suggestions how to solve this issue ?

Here is an example:

class Person {
    Boolean isPrimary

    static constraints = {
        isPrimary validator: isPrimaryValidator

        //or this for a fully qualified validator
        //isPrimary validator: Person.isPrimaryValidator
    }

    static isPrimaryValidator = { Boolean value, Person obj ->
        //some validation
    }
}

//in Test
Person.metaClass.'static'.isPrimaryValidator = { Boolean value, Person obj ->
     //Do something else
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM