在我的服务中尝试伪造服务器调用时获取方案 java.lang.NullPointerException: 方案
getting scheme java.lang.NullPointerException: scheme while trying to fake a server call in my service
我的项目中有一项服务 class,我想测试其中一种执行 api 调用的方法,因此我想捕获此调用并 return一些假的所以我可以测试我的方法,它看起来像这样:
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我去:
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
也试过放在 client 下 => :
val myService = new MyService {
implicit val config: Configuration = configuration
implicit val ws: WSClient = client
}
但是出现了一些其他错误,我在构造函数中没有足够的参数...
为什么它不起作用?
如果有更好更简单的方法来伪造这个 api 电话,我很想听听:)
谢谢!
Server.withRouter
可能不是您想要的。它创建一个服务器并将其绑定到每个实例的随机端口(除非您指定端口)。它还会创建自己的应用程序实例,该实例将与您用来实例化该服务的应用程序断开连接。
另一件事是注入的 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
}
}
}
}
}
}
我的项目中有一项服务 class,我想测试其中一种执行 api 调用的方法,因此我想捕获此调用并 return一些假的所以我可以测试我的方法,它看起来像这样:
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我去:
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
也试过放在 client 下 => :
val myService = new MyService {
implicit val config: Configuration = configuration
implicit val ws: WSClient = client
}
但是出现了一些其他错误,我在构造函数中没有足够的参数...
为什么它不起作用?
如果有更好更简单的方法来伪造这个 api 电话,我很想听听:)
谢谢!
Server.withRouter
可能不是您想要的。它创建一个服务器并将其绑定到每个实例的随机端口(除非您指定端口)。它还会创建自己的应用程序实例,该实例将与您用来实例化该服务的应用程序断开连接。
另一件事是注入的 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
}
}
}
}
}
}