Grails
  1. Grails
  2. GRAILS-9011

Validation exception thrown on adding object and inherited object to a collection-Domain Mocks - Unit test

    Details

    • Testcase included:
      yes

      Description

      when adding object and a sub-object to a collection (using domain mocks) a validation exception is being thrown

      we believe it may be related to this code change for GrailsDomainClassValidator https://github.com/grails/grails-core/commit/59b3613348d314213ac23c78cd085ef02252767b#grails-core/src/main/groovy/org/codehaus/groovy/grails/validation/GrailsDomainClassValidator.java
      code example below, attached full grails project

      class Group {
      	
      	String name
      	static hasMany = [members:Member]
      	static constraints = {
      	}
      	Collection <Member> members
      }
      
      class Member {
      
          static constraints = {
          }
      	String name
      }
      
      @ToString (includeNames=true,includeFields=true)
      class SubMemberA extends Member{
      
          static constraints = {
          }
      	String extraNameA
      }
      
      
      import grails.test.mixin.*
      import org.junit.*
      
      
      /**
       * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
       */
      @TestFor(Group)
      @Mock([Group,SubMemberA,Member])
      class GroupTests {
      
      	Group group
      	
      	@Before
      	void setUp() {
      		
      		group = new Group(name:"my group")
      		def subMemeberA = new SubMemberA(name:"my name A",extraNameA:"extra name A")
      		def member = new Member(name:"member")
      		
      		group.addToMembers subMemeberA
      		group.addToMembers member
      		group.save(failOnError:true)
      	}
      	@Test
      	void testStructure() {
      		
      		assertNotNull Group.findById(group.id)
      		
      	}
      	
      }
      

      Exception throws is

      Running 1 unit test... 1 of 1
      Failure: testStructure(test.GroupTests)
      java.lang.IllegalArgumentException: Argument [test.SubMemberA(extraNameA:extra name A)] is not an instance of [class test.Member] which this validator is configured for
      at org.grails.datastore.mapping.validation.ValidatingEventListener.doValidate(ValidatingEventListener.java:83)
      at org.grails.datastore.mapping.validation.ValidatingEventListener.beforeInsert(ValidatingEventListener.java:60)
      at org.grails.datastore.mapping.validation.ValidatingEventListener.onPersistenceEvent(ValidatingEventListener.java:47)
      at org.grails.datastore.mapping.engine.event.AbstractPersistenceEventListener.onApplicationEvent(AbstractPersistenceEventListener.java:46)
      at org.grails.datastore.mapping.engine.EntityPersister.cancelInsert(EntityPersister.java:228)
      at org.grails.datastore.mapping.engine.NativeEntryEntityPersister.executeInsert(NativeEntryEntityPersister.java:1370)
      at org.grails.datastore.mapping.engine.NativeEntryEntityPersister$1.run(NativeEntryEntityPersister.java:700)
      at org.grails.datastore.mapping.core.impl.PendingOperationExecution.executePendingOperation(PendingOperationExecution.java:33)
      at org.grails.datastore.mapping.core.AbstractSession.flushPendingOperations(AbstractSession.java:323)
      at org.grails.datastore.mapping.core.AbstractSession.flushPendingInserts(AbstractSession.java:315)
      at org.grails.datastore.mapping.core.AbstractSession.flush(AbstractSession.java:237)
      at org.grails.datastore.mapping.query.Query.flushBeforeQuery(Query.java:585)
      at org.grails.datastore.mapping.query.Query.list(Query.java:484)
      at org.grails.datastore.gorm.finders.AbstractFindByFinder.invokeQuery(AbstractFindByFinder.java:34)
      at org.grails.datastore.gorm.finders.AbstractFindByFinder$1.doInSession(AbstractFindByFinder.java:26)
      at org.grails.datastore.mapping.core.DatastoreUtils.execute(DatastoreUtils.java:301)
      at org.grails.datastore.gorm.finders.AbstractFinder.execute(AbstractFinder.java:40)
      at org.grails.datastore.gorm.finders.AbstractFindByFinder.doInvokeInternal(AbstractFindByFinder.java:24)
      at org.grails.datastore.gorm.finders.DynamicFinder.invoke(DynamicFinder.java:151)
      at org.grails.datastore.gorm.finders.DynamicFinder.invoke(DynamicFinder.java:352)
      at org.grails.datastore.gorm.GormStaticApi.methodMissing(GormStaticApi.groovy:108)
      at test.GroupTests.testStructure(GroupTests.groovy:32)

        Activity

        Hide
        Alexander Kose added a comment -

        Add another simple example.

        Show
        Alexander Kose added a comment - Add another simple example.
        Hide
        Yoann Trouillet added a comment -

        This issue is well documented - from two different developers - with tests attached. Could we please have this issue scheduled in 2.0.4?

        Show
        Yoann Trouillet added a comment - This issue is well documented - from two different developers - with tests attached. Could we please have this issue scheduled in 2.0.4?

          People

          • Assignee:
            Graeme Rocher
            Reporter:
            Tomer Shtilman
          • Votes:
            5 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development