我正在单元测试Grails控制器方法,该方法在内部创建用户实例。用户域类使用Spring Security插件的springSecurityService
对密码进行编码,然后再将其插入数据库。
有没有办法从我的单元测试中模拟springSecurityService
以消除该错误?
Failure: Create new individual member(MemberControllerSpec)
| java.lang.NullPointerException: Cannot invoke method encodePassword() on null object
请在下面找到我的单元测试。
@TestMixin(HibernateTestMixin)
@TestFor(MemberController)
@Domain([User, IndividualPerson])
class MemberControllerSpec extends Specification {
void "Create new individual member"() {
given:
UserDetailsService userDetailsService = Mock(UserDetailsService)
controller.userDetailsService = userDetailsService
def command = new IndividualPersonCommand()
command.username = 'scott@tiger.org'
command.password = 'What ever'
command.firstname = 'Scott'
command.lastname = 'Tiger'
command.dob = new Date()
command.email = command.username
command.phone = '89348'
command.street = 'A Street'
command.housenumber = '2'
command.postcode = '8888'
command.city = 'A City'
when:
request.method = 'POST'
controller.updateIndividualInstance(command)
then:
view == 'createInstance'
and:
1 * userDetailsService.loadUserByUsername(command.username) >> null
and:
IndividualPerson.count() == 1
and:
User.count() == 1
cleanup:
IndividualPerson.findAll()*.delete()
User.findAll()*.delete()
}
}
答案 0 :(得分:3)
模拟服务的一种方法是使用Groovy的MetaClass
import grails.test.mixin.Mock
import grails.plugin.springsecurity.SpringSecurityService
...
@Mock(SpringSecurityService)
class MemberControllerSpec extends Specification {
def setupSpec() {
SpringSecurityService.metaClass.encodePassword = { password -> password }
}
def cleanupSpec() {
SpringSecurityService.metaClass = null
}
....
在此示例中,对SpringSecurityService.encodePassword()
的调用只会以纯文本形式返回密码。
讨论了使用模拟的方法here。
答案 1 :(得分:1)
您可以使用此代码对User
中的密码进行编码:
def beforeInsert() {
encodePassword()
}
def beforeUpdate() {
if (isDirty('password')) {
encodePassword()
}
}
protected void encodePassword() {
password = springSecurityService?.passwordEncoder ? springSecurityService.encodePassword(password) : password
}
当springSecurityService
为空时,不会调用encodePassword
并且不会引发NPE
答案 2 :(得分:0)
在Grails v4 / v3中将控制器单元测试与spring security rest插件一起使用时,如果您的控制器方法引用了诸如“ athenticatedUser”之类的springSecurityService方法,则会出现NullPointException,因为springSecurityService不会自动连接到spring应用程序上下文中。 >
添加如下所示的代码,您可以注入springSecurityService并模拟其方法。
class GuessControllerSpec extends Specification implements ControllerUnitTest<GuessController> {
@Override
Closure doWithSpring() {
return {
// mock method
SpringSecurityService.metaClass.getCurrentUser = {return new User()}
// inject into spring context
springSecurityService(SpringSecurityService)
}
}
...
}