title: Grails 2.1.x Controller Unit Testing with services
body: <p>Attempting to unit test a Grails 2.1.x controller that calls a template to show a list of history items with a status. This controller works fine in manual testing but were attempting to automate things and I'm getting stuck on this part. Part of our issue may be that the domain object is over engineered.</p>
  
  <p>The setup for this test may be more integration rather than unit testing but I'm not sure I can test the function with out it.</p>
  
  <p>The controller action generates a list of history items via a createCriteria query. This list is passed to the template to be rendered. </p>
  
  <pre><code>def loadHistValues(){
      def histDomainObject = new historyDom()
      def elements = histDomainObject.createCriteria().list(max: params.max, offset: params.offset)
      render (template: 'searchResults', model:[elements: elements]
    }
  </code></pre>
  
  <p>The template has code that iterates through the list putting values in each column. One of these items is getStatus(). This calls a utility service to return the values.</p>
  
  <pre><code> def getStatus(){
       return historyUtillityService.getStatus(this)
   }
  </code></pre>
  
  <p>The service gets the latest history event and returns the value.</p>
  
  <pre><code> def getStatus(HistoryDom hist){
       def histStatus = HistoryEvent.createCriteria().get(
           maxResults(1)
           order('id', 'desc')
           eq('historyDom', hist)
        )
        if (histStatus == null)
            return 0
        else
            return histStatus.status
    }
  </code></pre>
  
  <p>I'm getting a null pointer when the getStatus() is called.</p>
  
  <p>I've setup both mock domain object and mock services but I'm not sure that these are getting down to this level or maybe I'm calling them wrong.</p>
  
  <pre><code>@TestFor (HistoryController)
  @MockFor (HistoryDom, HistoryEventDom)
  
  class HistoryControllerTests{
  
  def util = new UnitTestUtil()
  
  void testLoadHistValues(){
       def mockHistoryUtilityService = mockfor (HistoryUtilityService)
       mockHisotryUtilityService.demand.getStatus (-&gt; return Status.QUEUED)
  
       def histObj1 = util.initMockHistObj(1)
       def histObj2 = util.initMockHistObj(2)
  
       histObj1.save()
       histObj2.save()
  
       def mockHistEvent = new HistEvent(
           histDate: histObj1.getHistDate(),
           histObj: histObj1,
           histStatus: Status.QUEUED
       )
  
       mockHistEvent.save()
  
       controller.loadHistValues()
  
       assert response.text contains("Something worth testing")
   }
  </code></pre>
  
  <p>I tried setting a mock view before the call to the controller and checking the response text for that but it never gets past the call to the controller since its still trying to process the template. I'm at a loss at this point as to how to test this controller function, or is it that the object wasn’t architected properly; this seems a bit overly complicated.</p>