我试图模拟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 …Run Code Online (Sandbox Code Playgroud)