我正在尝试对 Spring Security Core Plugin 生成的用户域的控制器进行单元测试。控制器是使用 grails generate-all 生成的。该域有一个名为 springSecurityService 的瞬态属性。在我的单元测试中,我试图模拟该服务并将这个瞬态变量分配给我的模拟版本。但是,我收到此错误:
No such property: springSecurityService for class: com.myapp.security.SecUser Possible solutions:
springSecurityService groovy.lang.MissingPropertyException: No such property: springSecurityService for class:
com.myapp.security.SecUser at com.myapp.security.SecUserControllerTests.setUp(SecUserControllerTests.groovy:26)
这是我的域的样子:
class SecUser {
transient springSecurityService
String username
String password
boolean enabled
boolean accountExpired
....
def beforeInsert() {
encodePassword()
}
protected void encodePassword() {
password = springSecurityService.encodePassword(password)
}
}
这是我的测试的样子:
package com.myapp.security
import org.junit.*
import grails.test.mixin.*
import com.myapp.system.*
import grails.plugins.springsecurity.*
@TestFor(SecUserController)
@Mock([SecUser,SpringSecurityService])
class SecUserControllerTests {
@Before void setUp() {
def service = mockFor(SpringSecurityService)
service.demand.encodePassword(1..2) { a -> return 'd3jk3j4ls234'}
def control = service.createMock()
SecUser.springSecurityService = control
}
不确定我做错了什么,甚至我什至可以用瞬态属性做这样的事情?