关于如何最好地测试包含Akka Http Flow的Akka Stream的任何想法?我特别挣扎于以下方法:
def akkaHttpFlow(server: String)(implicit actorSystem: ActorSystem, actorMaterializer: ActorMaterializer) = {
val uri = new java.net.URI(server)
val port: Int = if( uri.getPort != -1) { uri.getPort } else { 80 }
Http().cachedHostConnectionPool[Seq[String]](uri.getHost, port)
.withAttributes(ActorAttributes.supervisionStrategy(decider))
}
这是测试代码
val emails = Set("tonymurphy@example.com")
val source: Source[String, NotUsed] = Source(emails)
val f = source
.grouped(10)
.via(requestBuilderFlow)
.via(akkaHttpFlow)
.map(responseHandler)
.runForeach(println)
f.futureValue.shouldBe(Done)
它失败并出现以下错误(非意外的tbh)>>>
The future returned an exception of type: akka.stream.StreamTcpException, with message: Tcp command [Connect(localhost:9001,None,List(),Some(10 seconds),true)] failed because of Connection refused.
是否可以在测试中嵌入akka http服务器?或者我如何最好地构建代码以便能够模拟它?
支持代码
object MyOperations extends StrictLogging {
val requestBuilderFunc : Seq[String] => (HttpRequest, Seq[String]) = { emails : Seq[String] =>
HttpRequest(method = HttpMethods.POST, uri = "/subscribers").withEntity(ContentTypes.`application/json`, ByteString(Json.toJson(emails).toString())) -> emails.toVector
}
val requestBuilderFlow : Flow[Seq[String],(HttpRequest, Seq[String]),NotUsed] = Flow[Seq[String]] map requestBuilderFunc
val responseHandler: ((Try[HttpResponse], Seq[String])) => (HttpResponse, Seq[String]) = {
case (responseTry, context) =>
logger.debug(s"Response: $responseTry")
(responseTry.get, context.asInstanceOf[Seq[String]])
}
}
我不得不承认我正在努力解决如何将我的scala应用程序组织成对象,特征,类,高阶函数等并测试它们
答案 0 :(得分:1)
您要做的是使用依赖注入之类的东西来注入CASE WHEN SUBSTRING([COUNTRY_CODE_NEW], 6,2) = '16' AND IS NOT NULL THEN 'ALBANIA'
。
在生产中,流程将来自akka http,但在测试中,您可以自己模拟它以返回您需要的任何内容。