本文整理汇总了Scala中org.mockito.Mockito.spy类的典型用法代码示例。如果您正苦于以下问题:Scala spy类的具体用法?Scala spy怎么用?Scala spy使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了spy类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: StatsFilterTest
//设置package包名称以及导入依赖的类
package com.twitter.finagle.http.filter
import com.twitter.finagle.Service
import com.twitter.finagle.http.{Request, Response}
import com.twitter.finagle.stats.InMemoryStatsReceiver
import com.twitter.util.{Await, Duration, Future, Time}
import org.junit.runner.RunWith
import org.scalatest.FunSuite
import org.scalatest.junit.JUnitRunner
import org.mockito.Mockito.{spy, verify}
@RunWith(classOf[JUnitRunner])
class StatsFilterTest extends FunSuite {
val service = new Service[Request, Response] {
def apply(request: Request): Future[Response] = {
val response = request.response
response.statusCode = 404
response.write("hello")
Future.value(response)
}
}
test("increment stats") {
val receiver = spy(new InMemoryStatsReceiver)
val filter = new StatsFilter(receiver) andThen service
Time.withCurrentTimeFrozen { _ =>
Await.result(filter(Request()), Duration.fromSeconds(5))
}
assert(receiver.counters(Seq("status", "404")) == 1)
assert(receiver.counters(Seq("status", "4XX")) == 1)
assert(receiver.stats(Seq("time", "404")) == Seq(0.0))
assert(receiver.stats(Seq("time", "4XX")) == Seq(0.0))
assert(receiver.stats(Seq("response_size")) == Seq(5.0))
}
test("status and time counters and stats are memoised") {
val receiver = spy(new InMemoryStatsReceiver)
val filter = new StatsFilter(receiver) andThen service
Time.withCurrentTimeFrozen { _ =>
Await.result(filter(Request()), Duration.fromSeconds(5))
Await.result(filter(Request()), Duration.fromSeconds(5))
}
// Verify that the counters and stats were only created once
verify(receiver).counter("status", "404")
verify(receiver).counter("status", "4XX")
verify(receiver).stat("time", "404")
verify(receiver).stat("time", "4XX")
verify(receiver).stat("response_size")
}
}