Grails
  1. Grails
  2. GRAILS-8112

@Mock breaks when domain has beforeInsert()/beforeUpdate()

    Details

      Description

      When unit testing a controller whose @Mock'd domain contains a beforeInsert() or beforeUpdate() method, the much anticipated Grails 2.0 in-memory GORM implementation breaks on all dynamic finders.

      Test app is attached, 3 files:
      User
      UserController
      UserControllerSpec (buildconfig has spock snapshot reference)

      running "test-app unit: com.test.UserControllerSpec" shows the problem

      and a successful find that should fail (since beforeInsert() MD5 encodes the password):
      println User.findByEmailAndPassword('abc@123.com', 'blah')

      confirms the bug

      Please let me know when this can be fixed, many unit tests are failing due to this unexpected behavior. in-memory GORM is such an awesome feature but I'm driving with the parking brake on right now ;-(

        Activity

        Hide
        Krzysztof Cygan added a comment -

        A sample app that might be related with problem, new gorm finders are not working in unit test

        Show
        Krzysztof Cygan added a comment - A sample app that might be related with problem, new gorm finders are not working in unit test
        Hide
        James Lang added a comment -

        Additionally, when attempting to update a domain instance that contains a beforeInsert/Update, the instance-to-update must be wrapped in a withNewSession {} block, otherwise unit test fails with:

        "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here"

        first 2 stacktrace lines:
        at org.grails.datastore.gorm.InstanceMethodInvokingClosure.call(GormEnhancer.groovy:241)
        at com.foo.User.beforeUpdate(User.groovy:34) // does isDirty('password') check prior to encoding

        When same page with same data is loaded via web browser, everything works as expected.

        Reset r = Reset.findByKey(key)
        if(r) {
        User u = User.get(r.user.id)
        u.password = r.password
        User.withNewSession { // withNewSession should not be required here
        if(!u.save(flush:true, validate:false))

        { i18n 'reset password fail' redirect controller:'reset', params:[email:email] return }

        }
        i18n 'reset password success'
        redirect controller:'reset'
        }

        Show
        James Lang added a comment - Additionally, when attempting to update a domain instance that contains a beforeInsert/Update, the instance-to-update must be wrapped in a withNewSession {} block, otherwise unit test fails with: "No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here" first 2 stacktrace lines: at org.grails.datastore.gorm.InstanceMethodInvokingClosure.call(GormEnhancer.groovy:241) at com.foo.User.beforeUpdate(User.groovy:34) // does isDirty('password') check prior to encoding When same page with same data is loaded via web browser, everything works as expected. Reset r = Reset.findByKey(key) if(r) { User u = User.get(r.user.id) u.password = r.password User.withNewSession { // withNewSession should not be required here if(!u.save(flush:true, validate:false)) { i18n 'reset password fail' redirect controller:'reset', params:[email:email] return } } i18n 'reset password success' redirect controller:'reset' }
        Hide
        Graeme Rocher added a comment -

        @James - I'm not sure i understand that, since Hibernate is not used for unit tests

        Show
        Graeme Rocher added a comment - @James - I'm not sure i understand that, since Hibernate is not used for unit tests
        Show
        Graeme Rocher added a comment - Fixed by https://github.com/SpringSource/grails-data-mapping/commit/62909610d00fe2e72748162141c1b0d304147212

          People

          • Assignee:
            Graeme Rocher
            Reporter:
            James Lang
          • Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development