Grails
  1. Grails
  2. GRAILS-9010

unit test - count() is not working on a class extending an abstract class (Domain Mocks)

    Details

    • Testcase included:
      yes

      Description

      The count() does not work properly on a domain object that extending an abstract class
      The object is saved but the count() always return zero
      After upgrading to 2.0.3 major part of our unit test failing due to this issue
      attached a simple test case that show this behaviour (full grails project in zip file)

      class Group {
      	String name
      	static hasMany = [members:Member]
          static constraints = {
          }
      	Collection members
      }
      
      import groovy.transform.ToString
      
      import java.util.Map
      
      
      @ToString (includeNames=true,includeFields=true,excludes="xmlProperties")
      abstract class Member   {
      	
      	
      	String name
      	String externalId
      }
      
      
      
      @ToString (includeNames=true,includeFields=true,excludes="xmlProperties")
      class SubMember extends Member {
      	
      	
      	String extraName
      }
      
      @TestFor(Group)
      @Mock([SubMember])
      class GroupTests {
      	Group group
      	SubMember subMember
      	
      	@Before
      	void setUp() {
      
      		group = new Group(name:"my group")
      		subMember = new SubMember(name:"my name",extraName:"extra name")
      		group.addToMembers subMember
      		group.save(failOnError:true)
      	}
      	@Test
      	void testSampleStructure()
      	{
      		//check that the group is saved
      		assertEquals 1, Group.count()
      		//get the saved group
      		Group savedGroup = Group.findById(group.id)
      		//check its not null
      		assertNotNull savedGroup
      		//check that it has 1 member
      		assertEquals 1, savedGroup.members.size()
      		//somehow the count is always zero		
      		assertEquals 1, SubMember.count()
      		 
      		
      	}
      	
      }
      
      

      Test result is :

      Failure: testSampleStructure(com.etrainu.GroupTests)
      java.lang.AssertionError: expected:<1> but was:<0>
      at org.junit.Assert.fail(Assert.java:93)
      at org.junit.Assert.failNotEquals(Assert.java:647)
      at org.junit.Assert.assertEquals(Assert.java:128)
      at org.junit.Assert.assertEquals(Assert.java:472)
      at org.junit.Assert.assertEquals(Assert.java:456)
      at com.etrainu.GroupTests.testSampleStructure(GroupTests.groovy:40)
      Completed 1 unit test, 1 failed in 9287ms

        Activity

        Hide
        Yoann Trouillet added a comment - - edited

        This bug is obvious and well documented within this ticket. Could we please have this issue scheduled in 2.0.4

        Show
        Yoann Trouillet added a comment - - edited This bug is obvious and well documented within this ticket. Could we please have this issue scheduled in 2.0.4
        Hide
        Graeme Rocher added a comment -
        Show
        Graeme Rocher added a comment - fixed by https://github.com/SpringSource/grails-data-mapping/commit/3839641ad1e7093ec9cf9a6b181efe349f40909e will require an update to datastore 1.0.8

          People

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

            Dates

            • Created:
              Updated:
              Resolved:

              Development