Grails
  1. Grails
  2. GRAILS-8395

DefaultMessage not set for unique field validation in unit tests

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: 2.0-RC2
    • Fix Version/s: None
    • Component/s: Testing
    • Labels:
    • Environment:
      Ubuntu 10.10, java-6-sun version "1.6.0_26" (64)

      Description

      The defaultMessage property does not seem to be set when validated in a unit test. Works fine when run through the console.

      Also note, if the test is run interactively, it will incorrectly pass validation on subsequent runs of test-app

      User.groovy
      package defaultmessage
      
      class User {
          
          String username
      
          static constraints = {
              username(unique:true)
          }
      }
      

      Failing test:

      UserTests.groovy
      package defaultmessage
      
      import grails.test.mixin.*
      import org.junit.*
      
      @TestFor(User)
      @Mock(User)
      class UserTests {
          
          void setUp() {
              new User(username:"test").save()
              println User.list()
          }
      
          void tearDown() {
              User.deleteAll()
          }
      
          void testUniqueErrorMessage() {
             def user = new User(username:"test")
             assert user.validate() == false
             assert user.errors['username'].defaultMessage == "Property [{0}] of class [{1}] with value [{2}] must be unique"
          }
      }
      
      | Failure:  testUniqueErrorMessage(defaultmessage.UserTests)
      |  Assertion failed: 
      
      assert user.errors['username'].defaultMessage == "Property [{0}] of class [{1}] with value [{2}] must be unique"
             |    |     |            |              |
             |    |     |            null           false
             |    |     Field error in object 'defaultmessage.User' on field 'username': rejected value [test]; codes [defaultmessage.User.username.unique.error.defaultmessage.User.username,defaultmessage.User.username.unique.error.username,defaultmessage.User.username.unique.error.java.lang.String,defaultmessage.User.username.unique.error,user.username.unique.error.defaultmessage.User.username,user.username.unique.error.username,user.username.unique.error.java.lang.String,user.username.unique.error,defaultmessage.User.username.unique.defaultmessage.User.username,defaultmessage.User.username.unique.username,defaultmessage.User.username.unique.java.lang.String,defaultmessage.User.username.unique,user.username.unique.defaultmessage.User.username,user.username.unique.username,user.username.unique.java.lang.String,user.username.unique,unique.defaultmessage.User.username,unique.username,unique.java.lang.String,unique]; arguments [username,class defaultmessage.User,test]; default message [null]
             |    org.grails.datastore.mapping.validation.ValidationErrors: 1 errors
             |    Field error in object 'defaultmessage.User' on field 'username': rejected value [test]; codes [defaultmessage.User.username.unique.error.defaultmessage.User.username,defaultmessage.User.username.unique.error.username,defaultmessage.User.username.unique.error.java.lang.String,defaultmessage.User.username.unique.error,user.username.unique.error.defaultmessage.User.username,user.username.unique.error.username,user.username.unique.error.java.lang.String,user.username.unique.error,defaultmessage.User.username.unique.defaultmessage.User.username,defaultmessage.User.username.unique.username,defaultmessage.User.username.unique.java.lang.String,defaultmessage.User.username.unique,user.username.unique.defaultmessage.User.username,user.username.unique.username,user.username.unique.java.lang.String,user.username.unique,unique.defaultmessage.User.username,unique.username,unique.java.lang.String,unique]; arguments [username,class defaultmessage.User,test]; default message [null]
             defaultmessage.User : null
      
      	at defaultmessage.UserTests.testUniqueErrorMessage(UserTests.groovy:26)
      | Completed 1 unit test, 1 failed in 2225ms
      | Packaging Grails application.....
      | Tests FAILED  - view reports in target/test-reports
      | Error Error running script test-app -echoOut: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManagerPostProcessor': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager': Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': Invocation of init method failed; nested exception is java.lang.ClassCastException: org.grails.datastore.gorm.validation.constraints.UniqueConstraint cannot be cast to org.codehaus.groovy.grails.orm.hibernate.validation.UniqueConstraint (Use --stacktrace to see the full trace)
      

      Working correctly through console:

      import defaultmessage.User
      new User(username:"test").save()
      
      def user = new User(username:"test")
      assert user.validate() == false
      assert user.errors['username'].defaultMessage == "Property [{0}] of class [{1}] with value [{2}] must be unique"
      

      Thanks.

        Activity

        Hide
        Graeme Rocher added a comment -

        Currently the validator configured is not setup to resolve messages from the bundles. We'll consider adding this as an improvement in 2.0.1

        Show
        Graeme Rocher added a comment - Currently the validator configured is not setup to resolve messages from the bundles. We'll consider adding this as an improvement in 2.0.1

          People

          • Assignee:
            Unassigned
            Reporter:
            Bill Thornton
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Last Reviewed:

              Development