MeIr
MeIr

Reputation: 7326

Grails Unit Test doesn't fail

I am trying to perform basic unit test on a Grails domain class.

Here is the domain class:

class User {
    String username
    String password
    String email

    static constraints = {
        username size: 4..15, blank: false, unique: true
        password size: 5..15, password: true, blank: false
        email email: true, blank: false
    }
}

Here is the unit test class:

@TestFor(User)
class UserTests {
    void testCreateUser() {
        def u = new User(username:"ab")
        assertFalse "There should be errors", u.validate()
        assertTrue "Should be errors here", u.hasErrors()
    }
}

username is constrained by size from 4 to 15. However, when I run grails test-app the above test succeeds. I don't understand why the constraint isn't causing it to fail.

Upvotes: 0

Views: 97

Answers (1)

Tomasz Kalkosiński
Tomasz Kalkosiński

Reputation: 3723

You didn't write which Grails version you use, but generally you should set up User class to be tested for constraint checks. Add this to your UserTests

def setUp() {
    mockForConstraintsTests(User)
}

Upvotes: 2

Related Questions