Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7308545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:34:19+00:00 2026-05-28T23:34:19+00:00

I have a custom tag lib that I wrote so that I can display

  • 0

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

<g:property label="Name" property="${user.name}"/>

Which keeps my views short and consistent as well as allows me to make changes quickly. My taglib code is as follows:

def property = {attrs, body ->
    def startingHtml = "..."
    def endingHtml = "..."

    out << startingHtml
    renderField(attrs.property)
    out << endingHtml
}

def renderField(property) {
    if (property instanceof Collection) {
        property.each { out << it + "</br>" }
    } else if(property instanceof Address){
        renderAddress(property)
    } else {
        out << property
    }
}

def renderAddress(address) {
    out << "Address Render Logic Here"
}

I’m trying to add some unit tests around this code because there is logic in it. Using the examples found here ( http://grails.org/doc/latest/guide/testing.html#unitTestingTagLibraries) 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.

@TestFor(PropertyTagLib)
class PropertyTagLibTests {

@Test
void propertyTagShouldRenderPropertyInsideOfTDWhenPropertyIsAString() {
    String result = applyTemplate('<g:property label="something" property="someTextValue"/>').trim()

    assert result.contains('someTextValue')
}

@Test
void propertyTagShouldRenderList() {
    String result = applyTemplate('g:property label="something" property="[\"one\", \"two\", \"three\"]"/>').trim()

    assert result.contains("one</br>two</br>three</br>")
}

@Test
void propertyTagShouldRenderPropertyInsideOfTDWhenPropertyIsAnAddress() {
    def address = new Address(line1: "Line 1")
    String result = applyTemplate('<g:property label="something" property="${address}"/>').trim()

    assert result.contains("Address Render Logic Here")
}
}

How can I test my taglib when one of the attributes is an object?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T23:34:20+00:00Added an answer on May 28, 2026 at 11:34 pm

    So I solved this by “cutting out the middle man” and circumventing the applyTemplate() and directly going to the method that does the logic. This isn’t quite ideal because of two reasons:
    1) I’m not asserting that the property tag is hooked up with renderField, but hopefully my other tests which still use applyTemplate() ensure that.
    2) In the real world out in a TagLib is a output writer and in my test I’ve created it as a list (I can do this since anything you can do a left shift on will suffice).

    What I do like about using a list is it helps assert order.

    @Test
    void renderFieldShouldRenderAddress() {
        def address = new Address(line1: "Line 1", line2: "Line 2", line3: "Line 3",
            city: "City", state: "ST", zipCode: "12345", country: "USA",
            buildingNumber: null, buildingName: null, roomNumber: null
            )
        def tagLib = new PropertyTagLib()
        def results = []
        tagLib.metaClass.getOut = {
            results
        }
    
        tagLib.renderField(address)
    
        assert "Line 1" == results[0]
        assert "<br />" == results[1]
        assert "Line 2" == results[2]
        assert "<br />" == results[3]
        assert "Line 3" == results[4]
        assert "<br />" == results[5]
        assert "City, ST 12345" == results[6]
        assert "<br />" == results[7]
        assert "USA" == results[8]
        assert "<br />" == results[9]
        assert 10 == results.size()
    }
    

    Any thoughts?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom tag that extends Spring's InputTag to display numbers in the
Basically I have a custom tag that handles querying a java object for me.
I have a custom tag, packaged into a library jar that is included in
I have created a custom tag that looks like this: def textField = {
Embedded custom-tag in dynamic content (nested tag) not rendering. I have a page that
I have a custom tag that I'm trying to pass a String to. It
I have a custom tag that does some processing and then sets a cookie.
I want to implement custom JSP list tag, but have problem with accessing properties
Have you had any success with uses of custom tag for view that increase
I have a problem: I need to build a custom tag, which can take

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.