简体   繁体   中英

Unit testing a Grails custom taglib based on built-in Grails taglib

I've an app based on Grails 1.3.7. And I need to write a unit test for a custom taglib that is based on the built-in taglib, <g:select /> to be specific.

I checked out the solution on this previous SO post but the solution stated is not working in my case (some properties are not being prooperly mocked up).

The other solution that I found was this . Using this approach, I get most of the properties of FormTagLib mocked up except for the grailsApplication property that select requires. The actual error that I get is Cannot invoke method getArtefact() on null object .

How can I properly write the unit test in such a case?

Edit
Here are my test class and the full stacktrace . Line #45 on the stacktrace is the call to the g.select from my custom taglib. My custom taglib is something like

def clientSpecificQueues = {attrs->
    def queueList = taskService.getClientSpecificQueues(session.clientName)
    def queueLabel = "Some String"


    if (queueList.size() > 0){
        out << queueLabel
    else
        out << g.select(name:'queueId', from: queueList, optionKey: 'id', optionValue: 'name')



}

I believe grailsApplication will never be mocked by default, it has too many responsibilities. I'd try to brutally patrially mock grailsApplication in setUp() by doing something like

tagLib.grailsApplication = [getArtefact: { -> return something suitable }]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM