title: Grails 2.0 Unit Testing Custom Tag Lib
body: <p>I have a custom tag lib that I wrote so that I can display properties of an object in an easy fashion.  It allows me to call</p>
  
  <pre><code>&lt;g:property label="Name" property="${user.name}"/&gt;
  </code></pre>
  
  <p>Which keeps my views short and consistent as well as allows me to make changes quickly.  My taglib code is as follows:</p>
  
  <pre><code>def property = {attrs, body -&gt;
      def startingHtml = "..."
      def endingHtml = "..."
  
      out &lt;&lt; startingHtml
      renderField(attrs.property)
      out &lt;&lt; endingHtml
  }
  
  def renderField(property) {
      if (property instanceof Collection) {
          property.each { out &lt;&lt; it + "&lt;/br&gt;" }
      } else if(property instanceof Address){
          renderAddress(property)
      } else {
          out &lt;&lt; property
      }
  }
  
  def renderAddress(address) {
      out &lt;&lt; "Address Render Logic Here"
  }
  </code></pre>
  
  <p>I'm trying to add some unit tests around this code because there is logic in it.  Using the examples found here ( <a href="http://grails.org/doc/latest/guide/testing.html#unitTestingTagLibraries" rel="nofollow">http://grails.org/doc/latest/guide/testing.html#unitTestingTagLibraries</a>) I started adding some tests.  The first two scenarios that my tag currently handles are String and Collection, which I was able to test correctly (first two tests below).  The last scenario that I need to test is an Address object (which is just a POGO with String attributes).  I can't seem to find a way to test passing an object as an attribute into a tag lib.</p>
  
  <pre><code>@TestFor(PropertyTagLib)
  class PropertyTagLibTests {
  
  @Test
  void propertyTagShouldRenderPropertyInsideOfTDWhenPropertyIsAString() {
      String result = applyTemplate('&lt;g:property label="something" property="someTextValue"/&gt;').trim()
  
      assert result.contains('someTextValue')
  }
  
  @Test
  void propertyTagShouldRenderList() {
      String result = applyTemplate('g:property label="something" property="[\"one\", \"two\", \"three\"]"/&gt;').trim()
  
      assert result.contains("one&lt;/br&gt;two&lt;/br&gt;three&lt;/br&gt;")
  }
  
  @Test
  void propertyTagShouldRenderPropertyInsideOfTDWhenPropertyIsAnAddress() {
      def address = new Address(line1: "Line 1")
      String result = applyTemplate('&lt;g:property label="something" property="${address}"/&gt;').trim()
  
      assert result.contains("Address Render Logic Here")
  }
  }
  </code></pre>
  
  <p>How can I test my taglib when one of the attributes is an object?</p>