title: How can I test my spring boot Service method that has an Authentication object as parameter?
body: <p>I am constructing a Spring Boot endpoint in Kotlin and I need to create tests for this. I could create a test for my controller class bellow:</p>
  
  <pre><code>import org.springframework.security.core.Authentication
  
  @RestController
  @RequestMapping("/servicos/solicitacoes")
  class SolicitacaoController {
  
  
      @Autowired
      private lateinit var solicitacaoService: SolicitacaoService
  
      @PostMapping(consumes = [MediaType.APPLICATION_JSON_VALUE])
      fun post(@RequestBody solicitacaoDTO: SolicitacaoDataClassDTO, @ApiIgnore authentication: Authentication): Solicitacao {
          return solicitacaoService.addSolicitacao(solicitacaoDTO, authentication)
      }
  }
  </code></pre>
  
  <p>However, as my service class has an Authentication parameter I don't know how I can create a test for my service class.</p>
  
  <p>This is my service class:</p>
  
  <pre><code>@Service
  class SolicitacaoService {
      @Autowired
      private lateinit var solicitacaoRepository: SolicitacaoRepository
  
      @Autowired
      private lateinit var acessoSistema: AcessoSistema
  
      fun addSolicitacao(solicitacaoDTO: SolicitacaoDataClassDTO, authentication: Authentication): Solicitacao{
          val idInstitucional = acessoSistema.getIdInstitucional(authentication)
          // Some Businnes logic
          return solicitacaoRepository.save(solicitacao)
      }
  }
  </code></pre>
  
  <p>The authentication process is made by a keycloak token, which the front-end needs to get to access back-end services.</p>
  
  <p>I can test my Controller, because in my post request I send the token in the header of the request. However, for testing the method addSolicitacao in my Service, I need to create an Authentication object.</p>
  
  <p>Any idea on how can I create a Test for my SolicitacaoService?
  I am using JUnit and Mockito. I thought about to mock my SolicitacaoRepository in my SolicitacaoServiceTest. However, I don't know how can I create an object Authentication with the parameters of my Keycloak server. </p>
  
  <p>This is my SolicitacaoServiceTest:</p>
  
  <pre><code>@RunWith(SpringRunner::class)
  //@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  class SolicitacaoServiceTest {
  
      @Autowired
      private lateinit var solicitacaoService: SolicitacaoService
  
      @MockBean
      private lateinit var solicitacaoRepository: SolicitacaoRepository
  
      @Test
      fun quandoAdicionarSolicitacaoComUsuarioComum_entaoSolicitacaoDeveSerRetornada(){
  
  
      }
  
  
  }
  </code></pre>