title: get current user in grails spock unit testing which uses spring security
body: <pre><code>package com.rohan
  
  import grails.test.mixin.*
  import spock.lang.Specification
  import grails.plugins.springsecurity.SpringSecurityService
  import grails.test.mixin.domain.DomainClassUnitTestMixin
  import com.rohan.meta.MetaServiceCategory
  
  @TestFor(ProjectDashBoardController)
              @Mock([SpringSecurityService,User,Project,MetaServiceCategory,ProjectIntroduction,ProjectDetails,ProjectAdditionalInfo,SPCompany,BuyerCompany,Conversation,ServiceCategory,UserAuthService,TimeSheetService,ProjectService])
  class ProjectDashBoardControllerSpec extends Specification {
  
      void "test timeSheets action"() {
  
          given:
          BuyerCompany buyerCompany = new BuyerCompany()
          Project testProject = new Project(title: "test    Project",projectId:"0411",buyerCompany:buyerCompany)
          testProject.save()
          println "project : "+testProject
  
          when:"When Project Id Given"
          controller.params.id = "00411"
          controller.timeSheets()
  
          then:
          response.redirectUrl.endsWith '/dashboard/index'
      }
  }
  </code></pre>
  
  <p>Here is controller code:</p>
  
  <pre><code>class ProjectDashBoardController {
  
      def springSecurityService
      def userAuthService
  
      def timeSheets(){
          def project=Project.findByProjectId(params.id)
          if(project){
              def user = userAuthService.getCurrentUser()
              // Do Something
          } else {
              redirect(controller:'dashboard',action:'index')
          }
       }
  }
  </code></pre>
  
  <p>@OPAL
  i think that's what u asking</p>
  
  <p>class UserAuthService {</p>
  
  <p>def getCurrentUser(){
          def user = springSecurityService.currentUser
          return user
      }
  }
  And finally I receive the following error:</p>
  
  <pre><code>Cannot get property 'currentUser' on null object
      at com.rohan.UserAuthService.getCurrentUser(UserAuthService.groovy:22)
      at com.rohan.ProjectDashBoardController.timeSheets(ProjectDashBoardController.groovy:130)
      at com.rohan.ProjectDashBoardControllerSpec.test timeSheets action(ProjectDashBoardControllerSpec.groovy:29) 
  </code></pre>