title: akka http mock (authorization) directive
body: <p>I'm trying to write unit testing to an akka-http server (route), but having trouble with the authorization directive. 
  I simply want to ignore (mock) the authorization directive.</p>
  
  <p>A minimal version of my code:</p>
  
  <pre><code>object App {
  
      val logger: Logger = Logger[App]
  
      def main(args: Array[String]): Unit = {
          implicit val system: ActorSystem = ActorSystem("my-system")
          implicit val executionContext: ExecutionContextExecutor = system.dispatcher
  
          val service = new TestRoute()
          val bindingFuture = Http().bindAndHandle(service.route, "0.0.0.0", 8080)
  
          bindingFuture.onComplete {
              case Success(_) =&gt; println(s"listening on localhost:8080")
              case Failure(error) =&gt; println(s"failed: ${error.getMessage}")
          }
      }
  
  
      object TestRoute {
  
          def authorize: Directive1[JsObject] = extractCredentials.flatMap {
              case Some(OAuth2BearerToken(token)) =&gt; verifyToken(token) match {
                  case Success(t) =&gt;
                      logger.debug("token verified successfully")
                      provide(t)
                  case Failure(t) =&gt;
                      logger.warn(s"token is not valid: ${t.getMessage}")
                      reject(Rejections.validationRejection(t.getMessage))
              }
              case _ =&gt;
                  logger.warn("no token present in request")
                  reject(AuthorizationFailedRejection)
          }
  
  
          def verifyToken(token: String): Try[JsObject] = {
              val source = Source.fromResource("public_production.pem")
              val fileData = try source.mkString finally source.close()
              JwtSprayJson.decodeJson(token, fileData, Seq(JwtAlgorithm.RS256))
          }
      }
  
      class TestRoute {
          val route: Route =
              TestRoute.authorize { jwtClaims =&gt;
                  println(jwtClaims)
                  complete("sucess")
              } 
      }
  }
  </code></pre>
  
  <p>I want to test the Route in <code>TestRoute</code>. So <code>authorize</code> is the directive I want to mock. I simply want to control what <code>authorize</code> will return, without having to actually getting in the function.</p>
  
  <p>My colleague suggested to wrap the Route in some kind of class that will take the <code>authorize</code> method as an argument, such that in the unit-test I can control it very easily. But I'm looking for the mock answer.</p>
  
  <p>I saw some examples with Mockito, but none of them were compatible with my scalatest version, which is <code>3.1.0</code>.</p>