samedi 31 octobre 2015

How to inject springSecurityService Into Grails Domain Class for Controller Unit Testing in Grails 2.4.2

How do i inject springSecurityService in Grails Domain class While unit testing Controller,Below is the Sample Code, i keep getting error can not get encodePassword on null Object and similarly for 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" 
    }

}

Aucun commentaire:

Enregistrer un commentaire