title: How to mock a service in grails TagLib unit test
body: <p>I have TagLib, Service and TestCase as follows</p>
  
  <p>How to mock a service in a taglib to get expected result from service</p>
  
  <p>TagLib:</p>
  
  <pre><code>   class SampleTagLib {
  
       static namespace = "sample"
       def baseService
  
       def writeName = { attrs, body -&gt;
          def result = baseService.findAttributeValue(attrs.something)
  
          if(result)
             out &lt;&lt; body()
       }
   }
  </code></pre>
  
  <p>Service:</p>
  
  <pre><code> class BaseService {
  
       def findAttributeValue(arg1) {
  
         return false  
       }
  }
  </code></pre>
  
  <p>TagLibUnitTestCase:</p>
  
  <pre><code>import spock.lang.*
  import grails.plugin.spock.*
  import org.junit.*
  import grails.test.mixin.*
  import grails.test.mixin.support.*
  
  import grails.test.mixin.Mock
  
  @TestFor(SampleTagLib)
  @Mock(BaseService)
  class SampleTagLibSpec extends Specification {
        def template
  
        def setup(){
  
           tagLib.baseService = Mock(BaseService)
        }
  
  
        def 'writeName'(){
          given:
          tagLib.baseService.findAttributeValue(_) &gt;&gt; true
          def template ='&lt;sample:writeName something='value'&gt;output&lt;/sample:writeName&gt;'
  
  
          when: 'we render the template'
          def output = applyTemplate(template, [sonething:'value')
  
          then: 'output'
          output =="output"
       }
   }
  </code></pre>
  
  <p>But it getting Error condition not satisfied. Getting output = " "</p>
  
  <p>Expected output = "output"</p>