grails / grails-testing-support

Trait-based testing library for Grails framework
http://testing.grails.org
Apache License 2.0
11 stars 21 forks source link

Grails 3.3.10: Testing shared constrints on command object fails #55

Closed almejo closed 5 years ago

almejo commented 5 years ago

Hi

This is an update to the #54 (I will close that one, the old one was using deprecated classes)

I am using grails 3.3.10. I think I found a bug when I try to test shared constraints in the command objects.

The constraint is not available to the command object and it throws an exception.

Using the shared constraints in a domain works well as expected.

Steps to Reproduce

The domain

class User {
    String firstName
    static constraints = {
        firstName shared: 'nonNullableString'
    }
    static mapping = {
        table '`user`'
    }
}

The command object inside a controller. This is my example

class AdminUserController {
    def save(UserCommand command) {
        if (command.hasErrors()) {
            render view: 'create', model: [command: command]
            return
        }
        new User(firstName: command.firstName).save()
        flash.success = message(code: 'default.created.message')
        redirect action: 'list'
    }
}

class UserCommand implements Validateable{
    String firstName

    static constraints = {
        firstName shared: 'nonNullableString'
    }
}

Than add the constraints in the application.groovy

grails.gorm.default.constraints = {
    nonNullableString(blank: false, maxSize: 255)
}

And then test

void testUserCommand() {
        when:
        UserCommand command = new UserCommand()

        then:
            !command.validate()
        command.errors['firstName'].code == 'nullable'
       }

Expected Behaviour

I expect the test to run

Actual Behaviour

The command object does not have the constraints during the test. I throws this exception

!command.validate()
| |
| grails.gorm.validation.exceptions.ValidationConfigurationException: Property [commandtests.UserCommand.firstName] references shared constraint [nonNullableString:null], which doesn't exist!
commandtests.UserCommand@1142f026

Environment Information

MacOS 10.14.4
Grails 3.3.10
JDK 1.8

Example Application

I created a simple project with the problem. It is available in this repository https://github.com/almejo/commandtest

jameskleeh commented 5 years ago

Fixed for both testing-support 1.1.6 and grails 3.3.11

almejo commented 5 years ago

Thanks @jameskleeh Is there a workaround or a tentative release date for the 3.3.11 version?

Is there an old version where that was working?

jameskleeh commented 5 years ago

You can wait for Grails 3.3.11, or testing support 1.1.6, or change your test to implement DataTest

swzaidi commented 5 years ago

I am facing same issue with grails 3.3.7 and grails-web-testing-support:1.1.4. I am implementing DataTest in the unit test but still seeing the error. @jameskleeh can you please clarify when you said "change your test to implement DataTest".