vendredi 22 janvier 2016

Grails3 unit test for domain class with derived property

I have the following Domain class with derived property lowercaseTag.

class Hashtag {
    String tag
    String lowercaseTag

    static mapping = {
        lowercase formula: 'lower(hashtag)'
    }
}

If I run the following unit test, it will fail on the last line, because lowercaseTag property is null and by default all properties have blank: false constraint.

@TestFor(Hashtag)
class HashtagSpec extends Specification {
    void "Test that hashtag can not be null"() {
        when: 'the hashtag is null'
        def p = new Hashtag(tag: null)

        then: 'validation should fail'
        !p.validate()

        when: 'the hashtag is not null'
        p = new Hashtag(tag: 'notNullHashtag')

        then: 'validation should pass'
        p.validate()
    }
}

The question is how to properly write unit tests in such cases? Thanks!

Aucun commentaire:

Enregistrer un commentaire