Friday 15 March 2013

grails spock testing failing with 'java.lang.IllegalArgumentException: ServletContext must not be null' -



grails spock testing failing with 'java.lang.IllegalArgumentException: ServletContext must not be null' -

i having method in command class, utilize messagesource.getmessage(...), messagesource won't injected commandclass. use

def messagesource = holders.applicationcontext.getbean("messagesource") within commandclass.

my problem when trying write unit test method,

@before void setup() { holders.applicationcontext.getbean("messagesource") } void "testfunction"() { //inside testfunction using messagesource given: //required things when: //call function then: //assert }

after testing function, getting error

java.lang.illegalargumentexception: servletcontext must not null @ grails.util.holders.getapplicationcontext(holders.java:80)

can suggest on how resolve one.

update

@validateable class commandclass { //required fields , constraints def formatdata(list<commandclass> commandobjs) { stringbuilder validationerrors commandobjs.each {commandobj-> validationerrors = new stringbuilder() if(commandobj.haserrors()) { commandobj.errors.allerrors.each {it -> validationerrors.append(messagesource.getmessage(it, null)).append('\n') } } commandobj.metaclass.validationerrors = validationerrors } } }

thanks in advance

i found answer

void setup() { mockapplicationcontext() } def static mockapplicationcontext() { grailsunittestmixin.initgrailsapplication() holders.grailsapplication = grailsunittestmixin.grailsapplication holders.metaclass.'static'.getapplicationcontext = { -> homecoming grailsunittestmixin.applicationcontext } holders.metaclass.applicationcontext.getbean = { bean -> homecoming grailsunittestmixin.messagesource } }

i 'll update more reply later

java grails spock

No comments:

Post a Comment