Grails
  1. Grails
  2. GRAILS-8965

Cryptic error message when constraint fails in unit test

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Duplicate
    • Affects Version/s: 2.0.1
    • Fix Version/s: 2.0.2
    • Component/s: Testing
    • Labels:
      None

      Description

      We were testing a simple domain class for which we thought we had defined the following constraints

      static constraints = {
          name(nullable: false, blank: false)
      }
      

      but they were really set to this

      static constraints = {
          name(nullable: false)
      }
      

      The test code checked for both error messages but by the time we reached

         assert !subject.validate()
         assert subject.errors['name'] == 'blank'
      

      we got this exception instead

      |  java.lang.NoClassDefFoundError: javassist/util/proxy/MethodHandler
      	at org.grails.datastore.mapping.model.AbstractMappingContext$DefaultProxyFactoryCreator.create(AbstractMappingContext.java:79)
      	at org.grails.datastore.mapping.model.AbstractMappingContext.getProxyFactory(AbstractMappingContext.java:61)
      	at org.grails.datastore.mapping.engine.EntityPersister.getProxyFactory(EntityPersister.java:67)
      	at org.grails.datastore.mapping.engine.EntityPersister.getObjectIdentifier(EntityPersister.java:97)
      	at org.grails.datastore.gorm.validation.constraints.UniqueConstraint.getIdentifier(UniqueConstraint.groovy:118)
      	at org.grails.datastore.gorm.validation.constraints.UniqueConstraint$_processValidate_closure1.doCall(UniqueConstraint.groovy:42)
      	at org.grails.datastore.gorm.validation.constraints.UniqueConstraint$_withManualFlushMode_closure2.doCall(UniqueConstraint.groovy:129)
      	at org.grails.datastore.gorm.GormStaticApi$_withSession_closure17.doCall(GormStaticApi.groovy:543)
      	at org.grails.datastore.mapping.core.DatastoreUtils.execute(DatastoreUtils.java:301)
      	at org.grails.datastore.gorm.AbstractDatastoreApi.execute(AbstractDatastoreApi.groovy:34)
      	at org.grails.datastore.gorm.GormStaticApi.withSession(GormStaticApi.groovy:542)
      	at org.grails.datastore.gorm.validation.constraints.UniqueConstraint.withManualFlushMode(UniqueConstraint.groovy:125)
      	at org.grails.datastore.gorm.validation.constraints.UniqueConstraint.processValidate(UniqueConstraint.groovy:39)
      	at grails.test.MockUtils$_addValidateMethod_closure87.doCall(MockUtils.groovy:1017)
      	at grails.test.MockUtils$_addValidateMethod_closure88.doCall(MockUtils.groovy:1060)
      	at com.acme.FooTests.testConstraints(FooTests.groovy:32)
      Caused by: java.lang.ClassNotFoundException: javassist.util.proxy.MethodHandler
      	at org.codehaus.groovy.tools.RootLoader.findClass(RootLoader.java:156)
      	at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
      	at org.codehaus.groovy.tools.RootLoader.loadClass(RootLoader.java:128)
      	at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
      	... 16 more
      

        Activity

        Hide
        Graeme Rocher added a comment -

        I believe this is already fixed, but it would help if attached an example

        Show
        Graeme Rocher added a comment - I believe this is already fixed, but it would help if attached an example
        Hide
        Andres Almiray added a comment -

        Here's more info. The problem is only reproducible in a plugin project, it should work fine in app project.

        Adding the following to BuildConfig.groovy makes it work

            plugins {
                runtime(":hibernate:$grailsVersion") {
                    export = false
                }
            }
        

        as mentioned in http://grails.1312388.n4.nabble.com/unique-constraint-test-works-when-in-grails-project-but-throws-exception-when-in-plugin-project-td4223635.html

        However, notice that we're adding a runtime dependency on hibernate to make the test work. Does that sound OK to you? I thought that mocks should work without requiring Hibernate at all.

        Also, this problem can be reproduced by setting a unique: constraint.

        Show
        Andres Almiray added a comment - Here's more info. The problem is only reproducible in a plugin project, it should work fine in app project. Adding the following to BuildConfig.groovy makes it work plugins { runtime( ":hibernate:$grailsVersion" ) { export = false } } as mentioned in http://grails.1312388.n4.nabble.com/unique-constraint-test-works-when-in-grails-project-but-throws-exception-when-in-plugin-project-td4223635.html However, notice that we're adding a runtime dependency on hibernate to make the test work. Does that sound OK to you? I thought that mocks should work without requiring Hibernate at all. Also, this problem can be reproduced by setting a unique: constraint.
        Hide
        Andres Almiray added a comment -

        Attached sample plugin project that demonstrates the error.
        Includes suggested fix in BuildConfig.groovy too.

        Show
        Andres Almiray added a comment - Attached sample plugin project that demonstrates the error. Includes suggested fix in BuildConfig.groovy too.
        Hide
        Graeme Rocher added a comment -

        the mocking APIs have a dependency on javassist, which is provided by hibernate. The better workaround is to add a 'test' time dependency on javassist

        Show
        Graeme Rocher added a comment - the mocking APIs have a dependency on javassist, which is provided by hibernate. The better workaround is to add a 'test' time dependency on javassist
        Hide
        Graeme Rocher added a comment -

        verified it is already fixed

        Show
        Graeme Rocher added a comment - verified it is already fixed
        Hide
        Andres Almiray added a comment -

        OK, I understand better now however the issue still persists. This 'quirk' of the mocking APIs in plugin projects should be IMO documented in the Grails Guide.

        Show
        Andres Almiray added a comment - OK, I understand better now however the issue still persists. This 'quirk' of the mocking APIs in plugin projects should be IMO documented in the Grails Guide.
        Hide
        Graeme Rocher added a comment -

        It isn't a quirk, it is a bug and we don't tend to document bugs

        Show
        Graeme Rocher added a comment - It isn't a quirk, it is a bug and we don't tend to document bugs

          People

          • Assignee:
            Graeme Rocher
            Reporter:
            Andres Almiray
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development