grails - How can I inject something into the session when unit testing a service -
i have written service reads cas session variable ...
package cp import edu.yale.its.tp.cas.client.filter.casfilter import javax.servlet.http.httpsession import org.springframework.web.context.request.requestcontextholder class authorizeservice { def username def loginerror def permissions def authcheck( string pagecontroller, string pageaction ) { username = getsession().getattribute(casfilter.cas_filter_user) ..... omitted rest of save space. ..... } private httpsession getsession() { return requestcontextholder.currentrequestattributes().getsession() }
i cannot figure out how put session in test piece of code run.
here's test:
package cp import grails.test.* class authorizeservicetests extends groovytestcase { def authorizeservice protected void setup() { super.setup() } protected void teardown() { super.teardown() } void testauthcheck() { def isauthorized // no username in session isauthorized = authorizeservice.authcheck( 'welcome', 'index' ) assertequals false, isauthorized // mock username rest of tests work mocksession["casfilter.cas_filter_user"] = "testuser" ..... omitted rest of save space. ..... }
when run test, here's error back:
no such property: mocksession class: cp.authorizeservicetests groovy.lang.missingpropertyexception: no such property: mocksession class: cp.authorizeservicetests @ cp.authorizeservicetests.testauthcheck(authorizeservicetests.groovy:26)
i've been searching google 2 days trying find way inject mocksession or similar can test service. can see pretty easy test if controller, appears services whole different animal.
as bit of background, i'm porting working php application grails ... i'm php guy, , first forray grails, apologize if noob question.
grails 2.3.4:
import grails.util.grailswebutil class sometests extends groovytestcase { void testsomething() { def request = grailswebutil.bindmockwebrequest() def mydummyobject = new dummyobject() request.session['mydummyobject'] = mydummyobject // run code , make asserts } }
Comments
Post a Comment