Injecting a mock service into a domain class for a Grails unit test?

▼魔方 西西 提交于 2019-12-05 04:49:25

What I ended up doing as a work around is,

SecUser.metaClass.encodePassword = { null }

In my domain object encodePassword is the method that uses the springSecurityService bean. So if I understand this correctly, this just overwrites this method to do nothing (the method is a void).

I'd prefer to use the metaClass to faux inject the bean, but my attempt at

SecUser.metaClass.getSpringSecurityService = { mockSpringSecurityService }

Blows up with an NPE when encodePassword attempts to use SpringSecurityService.

I feel like this is a less than ideal work around, so I'd love for someone to give a less hacky answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!