title: Grails Spock Mocking an Object
body: <p>I am new in unit-testing in Grails application using Spock. However I would like to ask  the following question. Lets say I want to run a test for the following function testfun.</p>
  
  <pre><code>class TestFun{
  
  boolean testfun(long userId, long orderId){
  
      User user = User.findByUserId(userId)
      if(user == null)return false
      Order order = Order.findByUserAndId(user, orderId)
  
      HashMap&lt;String, Object&gt; = orderContent   
      orderContent= order.orderContent // the order has an element orderContent for storing the elements that one user orders
  
      if(orderContent!=null){
        orderContent.put("meal",1)
        order.orderContent = orderContent
        return true
      }
  
      return false
  }
  }
  </code></pre>
  
  <p>The corresponding unit test in that case would be:</p>
  
  <pre><code>class TestFun extends Specification {
  
      def setup() {
  
          GroovySpy(User, global: true)
          GroovySpy(Order, global: true)
      }
   def "test funtest"() {
  
          User user = new User(2).save()
          Order order = new Order(3).save()
  
          when:
          service.testfun(2,3) == result
  
          then:
          2*User.findByUserId(2) &gt;&gt; Mock(User)
          1*Order.findByUserAndId(_ as User, 1)&gt;&gt; Mock(Order)
          result == true
  }
  }
  </code></pre>
  
  <p>However, I think that I have to mock the order.orderContent and I do not know how to mock it. Right now the test fails, because the orderContent is null so the testfun returns false.</p>
  
  <p>Can anyone help me on that?</p>