它显示对象创建不可能,因为成员wsClient:WSClient没有定义,什么用于
class ShoppingCartLoader extends LagomApplicationLoader{
override def load(context: LagomApplicationContext): LagomApplication = {
//Commenting AhcWSComponents is giving error here
new ShoppingCartApplication(context) {
override def serviceLocator: ServiceLocator = NoServiceLocator
}
}
}
abstract class ShoppingCartApplication(context: LagomApplicationContext)
extends LagomApplication(context)
/*with AhcWSComponents*/{
override lazy val lagomServer = serverFor[ShoppingCartService](wire[ShoppingCartImpl])
}
进入AhcWSComponents,它有:
lazy val wsClient: WSClient = {
implicit val mat = materializer
implicit val ec = executionContext
val asyncHttpClient = new AsyncHttpClientProvider(environment, configuration, applicationLifecycle).get
new AhcWSClientProvider(asyncHttpClient).get
}
如果需要,那么为什么它不包含在LagomApplication
本身中
LagomApplication
需要具有wsClient
,以便具有用于向外部服务发出HTTP请求的统一接口。 包括基于 AsyncHttpClient 的实现(通过AhcWSComponents
拉入(,但只要满足合同,就可以使用不同的实现。 因此,使用所包含的实现的决定是通过混合AhcWSComponents
来明确的
。当需要为依赖于向外部服务发出请求的功能编写测试时,会出现此要求的一个好处:您可以拥有一个模拟外部服务的实现并在所有测试中使用该实现,而不必在众多套件中设置相同的模拟。 但是,要利用这一点,您不希望在抽象类级别混合实现:
class ShoppingCartLoader extends LagomApplicationLoader {
override def load(context: LagomApplicationContext): LagomApplication = {
// as above until
new ShoppingCartApplication(context) with AhcWSComponents {
override def serviceLocator: ServiceLocator = NoServiceLocator
}
}
}
// now you don't need `with AhcWSComponents` on the abstract class
然后在测试中,当您需要LagomApplication
时:
new ShoppingCartApplication(context) {
override def wsClient: WSClient = ??? // mock implementation here
}