title: How to get the currentUser provided by spring security in Grails 2 unit testing
body: <p>Hi guys i am on trouble about getting the current user provided by spring.</p>
  
  <p>Here's my unit test code </p>
  
  <pre><code>   void "Test if adding project will sucess"() {
  
      given:
          def createProjectMock = mockFor(UserService)
  
          createProjectMock.demand.createNewProject { Map projectMap -&gt;
              return true
          }
  
          controller.userService = createProjectMock.createMock()
  
      when: "saveProject is execute"
  
          controller.saveProject()
  
      then: "page will to the list to view the saved project"
          response.redirectedUrl == '/user/index2'
  }
  </code></pre>
  
  <p>Here's my controller</p>
  
  <pre><code>def saveProject(ProjectActionCommand projectCmd) {
  
    def currentUser = springSecurityService.currentUser
    if (projectCmd.hasErrors()) {
  
      render view: 'createProject', model: [projectInstance: projectCmd, user:currentUser]
    } else {
  
      def getProjectMap = [:]
  
      getProjectMap = [
        projectName: params.projectName,
        user: currentUser
      ]
  
      def saveProject = userService.createNewProject(getProjectMap)
  
      if (saveProject) {   
  
        redirect view: 'index2'         
      } else {
        render 'Error upon saving'
      }
    }
  }
  </code></pre>
  
  <p>And here's my service</p>
  
  <pre><code>  Project createNewProject(Map projectMap){
      def createProject = new Project()
  
      createProject.with {
        projectName = projectMap.projectName
        user = projectMap.user
      }
  
      createProject.save(failOnError:true, flush: true)
    }
  </code></pre>
  
  <p>And i always getting this error:</p>
  
  <p>Cannot get property 'currentUser' on null object.</p>
  
  <p>Hope you can help me. Thanks </p>