我如何在 Grails 域类中注入 springSecurityService 在单元测试控制器时,以下是示例代码,我不断收到错误 can not get encodePassword on null Object 和 getPrincipal();
//Controller class
class UserController{
def save(){
def user=new AppUser(params).save();
render(user as JSON)
}
}
//Domain Class
class User{
transient springSecurityService
String name
String address
String password
String createdBy
def beforeInsert(){
password=springSecurityService.encodePassword(password);
def principal = springSecurityService.getPrincipal()
}
//Controller Test
@TestMixin(GrailsUnitTestMixin)
@TestFor(UserController)
class UserControllerSpec {
void setUp() {
}
void "test save"(){
given:
params.name="A"
params.password="abc"
params.address="XYZ"
when:
controller.save();
then:
response.status=="200"
}
}