Grails
  1. Grails
  2. GRAILS-7438

mockDomain does not register subclasses' meta-classes

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.3.7
    • Fix Version/s: 2.0 final
    • Component/s: Testing
    • Labels:
      None
    • Patch Submitted:
      Yes
    • Testcase included:
      yes

      Description

      When the list supplied to GrailsUnitTestCase's mockDomain method contains domain objects that are of a subtype of the mocked domain class, their meta-classes are enhanced (e.g. dynamic finders are added), but registerMetaClass is never called for those subclasses. This results in unexpected behaviour in integration tests, where the dynamic methods are expected to run 'normally', i.e. against the in-memory database. The attached project shows this: grails test-app :integration works, whereas grails test-app doesn't, because the unit test uses mockDomain.

      A possible fix would be to replace the first line of mockDomain (registerMetaClass(domainClass)) with

      ([domainClass] + instances.collect {it.class}).unique().each {domainClassToRegister ->
          registerMetaClass(domainClassToRegister)
      }
      

        Activity

        Hide
        Bob Herrmann added a comment - - edited

        Ouch! This bit me hard in the ass. The integration tests would fail with error,

        org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value: SomeClass.InMyProject

        Which might have just said "Something is wrong somewhere, good luck with that...." Took me about five hours to reduce it to a simple "registerMetaClass(...)" on each of my subclasses. (Not entirely because I'm especially dull, but in part because I was adding in a bunch of unit tests which worked fine alone but would break with the integration tests. I had to do a binary search on what was breaking the integration run. The integration run takes 3 minutes to run.)

        Show
        Bob Herrmann added a comment - - edited Ouch! This bit me hard in the ass. The integration tests would fail with error, org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value: SomeClass.InMyProject Which might have just said "Something is wrong somewhere, good luck with that...." Took me about five hours to reduce it to a simple "registerMetaClass(...)" on each of my subclasses. (Not entirely because I'm especially dull, but in part because I was adding in a bunch of unit tests which worked fine alone but would break with the integration tests. I had to do a binary search on what was breaking the integration run. The integration run takes 3 minutes to run.)
        Hide
        Graeme Rocher added a comment -

        This is fixed in 2.0 where we have a new MetaClassRegistryCleaner that eliminates the need for registerMetaClass

        Show
        Graeme Rocher added a comment - This is fixed in 2.0 where we have a new MetaClassRegistryCleaner that eliminates the need for registerMetaClass

          People

          • Assignee:
            Unassigned
            Reporter:
            Levi Hoogenberg
          • Votes:
            1 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:
              Last Reviewed:

              Development