我正在尝试模拟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)
阅读here,似乎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
}
}
}
我的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",
[...]
但我找不到:org.scalamock.annotation.mockObject
也许这可以使用EasyMock和PowerMock完成,但我找不到任何Scala示例代码。
有什么想法吗?
答案 0 :(得分:11)
使用ScalaMock 3模拟单例对象不可能,但Paul Butcher希望在ScalaMock 4中重新引入此功能(参见http://paulbutcher.com/2014/04/15/scalamock-status-report/)
答案 1 :(得分:3)
不要嘲笑单身人士。而不是WS,使您的服务组件依赖于隐藏它的瘦外观:
trait GeolocationService {
def ws: (String, Seq[String]) => Promise[Response] = { (url, params) =>
wsClient.url(serviceProviderEndpoint.get).withQueryString(params: _*).get()
}
def getPath(origin: Location, destination: Location): Future[Route]
}
并且在测试中,只需使用模拟覆盖ws方法,现在很容易创建:
val mockedWs = mock[(String, Seq[String]) => Promise[Response]]
// TODO specify mock's behavior here
val service = new DefaultGeolocationService() {
override def ws = mockedWs
}
答案 2 :(得分:0)
为什么不使用蛋糕图案?
它有点冗长,但它会解决你的嘲弄问题。
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
}
您也可以使用monads来执行此操作,但我从未实现过,它在此处描述: Scala dependency injection: alternatives to implicit parameters
答案 3 :(得分:0)
我不想改变设计以适应这种限制。基本上我能得到的就是改变设计,然后使用模拟框架
//since mocking frameworks on SCALA is not support mocking singleton
//hacks are either required to change the design or ...
//I'd rather putting the mocking logic here by myself
var testUser:Option[User] = None;
def getCurrentUser(request: Request[Object]) = {
if( testUser != None ){
testUser;
}
希望它有所帮助。