Announcement Announcement Module
Collapse
No announcement yet.
addTo* not working with mockDomain Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • addTo* not working with mockDomain

    I'm having a problem getting addTo* method to work properly with mocked domains.

    I'm using Win7 x64, java 1.6.0.21 and grails 1.3.4.

    Here are my test classes (Foo and Bar are domain, and Alpha is a service)

    Code:
    package alpha
    
    class Foo {
    
        String name
        
        static hasMany = [ bars : Bar ]
        
        static constraints = {
        }
        
        String toString() {
        	name
        }
    }
    Code:
    package alpha
    
    class Bar {
    
        String name
        
        static belongsTo = [ foo : Foo ]
        
        static constraints = {
        }
        
        String toString() {
        	name
        }
    }
    Code:
    package alpha
    
    class AlphaService {
    
        static transactional = true
    
        def load() {
        
        	def foo1 = new Foo(name:"foo1")
        	foo1.addToBars new Bar(name:"bar11")
        	foo1.addToBars new Bar(name:"bar12")
    		foo1.save()
    		
        	def foo2 = new Foo(name:"foo2")
        	foo2.addToBars new Bar(name:"bar21")
        	foo2.addToBars new Bar(name:"bar22")
    		foo2.save()
    		
        }
    }
    Code:
    package alpha
    
    import grails.test.*
    
    class AlphaServiceTests extends GrailsUnitTestCase {
        
        def service
        
        protected void setUp() {
            super.setUp()
    
    		mockDomain Foo
    		mockDomain Bar
    
            service = new AlphaService()
        }
    
        protected void tearDown() {
            super.tearDown()
        }
    
        void testLoad1() {
    		
    		service.load()
    		
    		assertEquals 2, Foo.list().size()
    		assertEquals 4, Bar.list().size()  // fails - expected:<4> but was:<0>
    		
        }
    
        void testLoad2() {
    		
        	def foo1 = new Foo(name:"foo1")
        	foo1.addToBars new Bar(name:"bar11")
        	foo1.addToBars new Bar(name:"bar12")
    		foo1.save()
    		
        	def foo2 = new Foo(name:"foo2")
        	foo2.addToBars new Bar(name:"bar21")
        	foo2.addToBars new Bar(name:"bar22")
    		foo2.save()
    		
    		assertEquals 2, Foo.list().size()
    		assertEquals 4, Bar.list().size()  // fails - expected:<4> but was:<0>
    		
        }
    
        void testLoad3() {
    		
        	def foo1 = new Foo(name:"foo1")
        	foo1.addToBars new Bar(foo:foo1, name:"bar11")
        	foo1.addToBars new Bar(foo:foo1, name:"bar12")
    		foo1.save()
    		
        	def foo2 = new Foo(name:"foo2")
        	foo2.addToBars new Bar(foo:foo2, name:"bar21")
        	foo2.addToBars new Bar(foo:foo2, name:"bar22")
    		foo2.save()
    		
    		assertEquals 2, Foo.list().size()
    		assertEquals 4, Bar.list().size()  // fails - expected:<4> but was:<0>
    		
        }
    
        void testLoad4() {
    		
        	def foo1 = new Foo(name:"foo1")
    		foo1.save()
    
        	foo1.addToBars new Bar(name:"bar11")
        	foo1.addToBars new Bar(name:"bar12")
    		foo1.save()
    		
        	def foo2 = new Foo(name:"foo2")
    		foo2.save()
    
        	foo2.addToBars new Bar(name:"bar21")
        	foo2.addToBars new Bar(name:"bar22")
    		foo2.save()
    		
    		assertEquals 2, Foo.list().size()
    		assertEquals 4, Bar.list().size()  // fails - expected:<4> but was:<0>
    		
        }
    
        void testLoad5() {
    		
        	def foo1 = new Foo(name:"foo1")
        	foo1.addToBars new Bar(name:"bar11").save()
        	foo1.addToBars new Bar(name:"bar12").save()
    		foo1.save()
    		
        	def foo2 = new Foo(name:"foo2")
        	foo2.addToBars new Bar(name:"bar21").save()
        	foo2.addToBars new Bar(name:"bar22").save()
    		foo2.save()
    		
    		assertEquals 2, Foo.list().size()
    		assertEquals 4, Bar.list().size()  // fails - expected:<4> but was:<0>
    		
        }
    
        void testLoad6() {
    		
        	def foo1 = new Foo(name:"foo1")
        	foo1.addToBars new Bar(foo:foo1, name:"bar11").save()
        	foo1.addToBars new Bar(foo:foo1, name:"bar12").save()
    		foo1.save()
    		
        	def foo2 = new Foo(name:"foo2")
        	foo2.addToBars new Bar(foo:foo2, name:"bar21").save()
        	foo2.addToBars new Bar(foo:foo2, name:"bar22").save()
    		foo2.save()
    		
    		assertEquals 2, Foo.list().size()
    		assertEquals 4, Bar.list().size()  // success!
    		
        }
    
    }
    When I run the tests, only testLoad6 passes. All of the other tests fail, indicating that no Bar objects have been saved.
    Is there something I'm doing wrong with my code?
    Can someone verify that they see the same problem?

    Thanks,
    Paul Woods

  • #2
    You're running into the limitations of the mocking framework. It works well in many situations, but it does not currently emulate the whole behaviour of GORM. In this particular case, the mock addToBars() method is not automatically 'saving' the Bar instances, hence why Bar.list() returns no results.

    testLoad6() works because it explicitly saves the Bar instances, so they are added to the underlying list. You might expect testLoad5() to work for the same reason, but the save() fails due to a validation error: the 'foo' back reference cannot be null.

    If you're doing lots of database work in a service or controller, then you are probably better off with an integration test. This is particularly the case if you want to ensure that your queries are working correctly.

    Comment


    • #3
      Ok. I'll move these types of tests into integration tests.

      Thanks,
      Paul

      Comment

      Working...
      X