2017-03-29 10 views
0

私は自分のプロジェクトにサービスクラスを持っており、そのメソッドのうちの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の呼び出しがあった場合、私はそれを聞いて:)

感謝を愛します!

答えて

1

Server.withRouterあなたが望むものと正確に一致しない場合があります。サーバーを作成し、インスタンスごとにランダムなポートにバインドします(ポートを指定しない限り)。また、サービスのインスタンス化に使用したアプリケーションからが切断されたという独自のアプリケーションインスタンスも作成されます。

もう1つのことは、注入されたWSClientでないことです。代わりにWsTestClient.withClientブロックに渡されるclientを使用する必要があります。だから、あなたのような何かをする必要があります:

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] 

    "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 => 

      // Use the client "instrumented" by Play. It will 
      // handle the relative aspect of the url. 
      val myService = new MyService(client, config) 

      whenReady(myService.methodToTest(List("1","2","3"))) { res => 
      res.isRight shouldBe true 
      } 
     } 
     } 
    } 
    } 
} 
関連する問題