如何模拟 Scala 单例对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25293758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to mock a Scala singleton object?
提问by user2664655
I am trying to mock a Scala singleton object. In particular, I need to mock the object play.api.libs.ws.WSused inside a service component (class under test).
Using Mockito this is not possible, the test execution fails in the following way:
我正在尝试模拟 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)
Reading here, it seems that Scalamock allows to do it:
在这里阅读,似乎 Scalamock 允许这样做:
To mock a standalone singleton object, use
org.scalamock.annotation.mockObject.
要模拟独立的单例对象,请使用
org.scalamock.annotation.mockObject.
My service component is something like this:
我的服务组件是这样的:
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
}
}
}
My build.sbt has all these dependencies:
我的 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",
[...]
but I cannot find this: org.scalamock.annotation.mockObject
但我找不到这个: org.scalamock.annotation.mockObject
Probably this can be done also using EasyMock and PowerMock, but I cannot find any Scala example code.
可能这也可以使用 EasyMock 和 PowerMock 来完成,但我找不到任何 Scala 示例代码。
Any idea?
任何的想法?
回答by n_l
Mocking singleton objects using ScalaMock 3 is not possible, however Paul Butcher expects to reintroduce this feature in ScalaMock 4 (see http://paulbutcher.com/2014/04/15/scalamock-status-report/)
使用 ScalaMock 3 模拟单例对象是不可能的,但是 Paul Butcher 希望在 ScalaMock 4 中重新引入这个特性(参见http://paulbutcher.com/2014/04/15/scalamock-status-report/)
回答by Przemek Pokrywka
Don't mock the singleton. Instead of WS, make your service component depend on a thin facade hiding it:
不要嘲笑单身人士。而不是 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]
}
and in your test, just override ws method with a mock, which is now easy to create:
在您的测试中,只需使用模拟覆盖 ws 方法,现在很容易创建:
val mockedWs = mock[(String, Seq[String]) => Promise[Response]]
// TODO specify mock's behavior here
val service = new DefaultGeolocationService() {
override def ws = mockedWs
}
回答by zinking
I don't like to change the design to accommodate this kind of limitation. basically what I can get is all to change the design and then use the mock framework
我不喜欢改变设计来适应这种限制。基本上我能得到的就是改变设计然后使用模拟框架
//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;
}
hope it helps.
希望能帮助到你。
回答by dirceusemighini
Why you don't use the cake pattern?
你为什么不使用蛋糕图案?
It's a little verbose but it will solve your mocking problem.
这有点冗长,但它会解决你的嘲笑问题。
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
}
You can also use monads to do this, but I have never implemented, it's described here: Scala dependency injection: alternatives to implicit parameters
您也可以使用 monads 来做到这一点,但我从未实现过,这里有描述: Scala 依赖注入:隐式参数的替代方案

