私は自分のプロジェクトにサービスクラスを持っており、そのメソッドのうちの1つをAPI呼び出しをテストしたいので、スキームを取得するjava.lang.NullPointerException:私のサービスでサーバコールを偽装しようとしているときのスキーム
class MyService @Inject()(implicit config: Configuration, wsClient: WSClient) {
def methodToTest(list: List[String]): Future[Either[BadRequestResponse, Unit]] = {
wsClient.url(url).withHeaders(("Content-Type", "application/json")).post(write(list)).map { response =>
response.status match {
case Status.OK =>
Right(logger.debug("Everything is OK!"))
case Status.BAD_REQUEST =>
Left(parse(response.body).extract[BadRequestResponse])
case _ =>
val ex = new RuntimeException(s"Failed with status: ${response.status} body: ${response.body}")
logger.error(s"Service failed: ", ex)
throw ex
}
}
}
}
、今、私のテストクラスで私が行く:
class MyServiceTest extends FreeSpec with ShouldMatchers with OneAppPerSuite with ScalaFutures with WsScalaTestClient {
implicit lazy val materializer: Materializer = app.materializer
lazy val config: Configuration = app.injector.instanceOf[Configuration]
lazy val myService = app.injector.instanceOf[MyService]
"My Service Tests" - {
"Should behave as im expecting" in {
Server.withRouter() {
case POST(p"/fake/api/in/conf") => Action { request =>
Results.Ok
}
} { implicit port =>
WsTestClient.withClient { implicit client =>
whenReady(myService.methodToTest(List("1","2","3"))) { res =>
res.isRight shouldBe true
}
}
}
}
}
}
と、私はこのエラーを取得:それはこのようになりますので、私は私のメソッドをテストすることができ、この呼び出しをキャッチし、偽の何かを返す
scheme java.lang.NullPointerException: scheme
もクライアント=>下に置かれてみました:
val myService = new MyService {
implicit val config: Configuration = configuration
implicit val ws: WSClient = client
}
が、私はコンストラクタで十分な議論を持っていけないいくつかの他のエラー...なぜそれが機能していない
を得ましたか。
偽物へのより良いND簡単な方法は、このAPIの呼び出しがあった場合、私はそれを聞いて:)
感謝を愛します!