use*_*655 20 testing singleton scala mocking playframework
我试图模拟Scala单例对象.特别是,我需要模拟play.api.libs.ws.WS
服务组件(被测试的类)中使用的对象.使用Mockito这是不可能的,测试执行失败的方式如下:
[error] MockitoException: :
[error] Cannot mock/spy class play.api.libs.ws.WS$
[error] Mockito cannot mock/spy following:
[error] - final classes
[error] - anonymous classes
[error] - primitive types (GeolocationSpec.scala:18)
Run Code Online (Sandbox Code Playgroud)
在这里阅读,似乎Scalamock允许这样做:
要模拟独立的单例对象,请使用
org.scalamock.annotation.mockObject
.
我的服务组件是这样的:
trait GeolocationService {
def wsClient = WS
def getPath(origin: Location, destination: Location): Future[Route]
}
class DefaultGeolocationService extends GeolocationService {
val serviceProviderEndpoint = Play.current.configuration.getString("api.directions.endpoint")
override def getPath(origin: Location, destination: Location): Future[Route] = {
val params = Seq(
"origin" -> s"${origin.lat},${origin.lon}",
"destination" -> s"${destination.lat},${destination.lon}"
);
val resp = wsClient.url(serviceProviderEndpoint.get).withQueryString(params: _*).get()
resp.map {
// omitted code
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的build.sbt具有所有这些依赖项:
[...]
"org.scalatest" %% "scalatest" % "2.2.1",
"org.specs2" %% "specs2" % "2.3.13" % "test",
"org.scalamock" %% "scalamock-specs2-support" % "3.0.1" % "test",
"org.scalamock" %% "scalamock-scalatest-support" % "3.0.1" % "test",
"org.scalamock" %% "scalamock" % "3.0.1",
[...]
Run Code Online (Sandbox Code Playgroud)
但我找不到这个: org.scalamock.annotation.mockObject
也许这也可以使用EasyMock和PowerMock完成,但我找不到任何Scala示例代码.
任何的想法?
n_l*_*n_l 11
使用ScalaMock 3模拟单例对象是不可能的,但Paul Butcher希望在ScalaMock 4中重新引入此功能(请参阅http://paulbutcher.com/2014/04/15/scalamock-status-report/)
为什么不使用蛋糕图案?
它有点冗长,但它会解决你的模拟问题。
trait GeolocationServiceComponent {
val geolocationService:GeolocationService
trait GeolocationService {
def wsClient
def getPath(origin:Location, destination: Location): Future[Route]
}
}
trait GeolocationServiceComponentImpl {
val geolocationService = new GeolocationService {
override def wsClient = WS
}
}
class DefaultGeolocationService extends GeolocationServiceComponent ...
//Defined into your test class
trait MockGeolocationServiceComponent {
val geolocationService = Mock[GeolocationService]
//Here you define your mock logic
}
Run Code Online (Sandbox Code Playgroud)
你也可以使用 monad 来做到这一点,但我从未实现过,它的描述如下: Scala 依赖注入:隐式参数的替代方案
归档时间: |
|
查看次数: |
11518 次 |
最近记录: |