Grails unit testing service with spock mocking functionality -
presently trying test grails service (experience geb functional testing)
trying mock necessary required data service need (e.g. user,etc.) ... no matter how seem declare/initialize domains of interest all appear null in tests methods
i'm trying set required information once, can reused through test methods.
@testfor(reminderservice) @mock([reminder, user, subscriptions, organisation, userorganisation, organisationrole, ref, role, title]) class reminderservicetests extends specification { @shared user, org, userorg, roleuser, sub, orgrole, ti, refvals, reminder def mailservice def setup() { def refsubstatus = new ref(value: 'current').save() def refsubpublic = new ref(value: 'no').save() def refsubtype = new ref(value: 'subscription taken').save() def refremunit = new ref(value: 'month').save() def reforgrole = new ref(value: 'subscriber').save() def refremmethod = new ref(value: 'email').save() def refremtrigger = new ref(value: 'subscription manual renewal date').save() reminder = new reminder(user: user, active: true, amount: 1, unit:refremunit, remindermethod:refremmethod, trigger: refremtrigger, lastran:null).save() refvals = [refsubstatus,refsubpublic, refsubtype, refremunit, reforgrole, refremmethod, refremtrigger] roleuser = new role(authority: 'basic_user', roletype:'global').save() ti = new title(title: "a random title....", impid: uuid.randomuuid().tostring()).save() sub = new subscription(name:"a random subscription name", status:refsubstatus, identifier:uuid.randomuuid().tostring(), impid:uuid.randomuuid().tostring(), startdate:new localdate().minusyears(1).todate(), enddate: new localdate().plusmonths(1).todate(), ispublic: refsubpublic, type: refsubtype, renewal: new localdate().minusmonths(3).todate()).save() org = new organisation(name: "new org", impid: uuid.randomuuid().tostring()).save() orgrole = new organisationrole(sub: sub, roletype: reforgrole, org: org).save() user = new user(username: 'j_doe', firstname: "john", lastname: "doe", email: 'example@googlemail.com', defaultdash: org).save() userorg = new userorganisation(org: org, user: user, formalrole: roleuser, status: 1).save() mailservice = new mailservice() // mockdomain(ref, refdatavalues) // mockdomain(title, ti) // mockdomain(organisationrole, orgrole) // mockdomain(organisation, org) // mockdomain(user, user) // mockdomain(userorganisation, userorg) // mockdomain(reminder, reminder) } def "getting subscriptions user"() { when: def subscriptions = service.getauthorisedsubsciptionsbyuser(user) then: subscriptions != null subscriptions.size() > 0 }
everything null, i've tried using mockdomain (see commented out section in setup()
, including using setupspec()
not quite right)
have tried not using @shared? see now, don't need it. references shared throughout lifecycle of spec, put init code in setup, called before each spec. not seem right. @shared variables internally stored in different instance normal variables, there may kind of mixup. also, might try use setupspec() instead of setup() @shared variables.
Comments
Post a Comment