本文整理汇总了Scala中org.mockito.Matchers类的典型用法代码示例。如果您正苦于以下问题:Scala Matchers类的具体用法?Scala Matchers怎么用?Scala Matchers使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Matchers类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: RefcountedServiceTest
//设置package包名称以及导入依赖的类
package com.twitter.finagle.service
import com.twitter.util._
import org.scalatest.FunSuite
import org.scalatest.junit.JUnitRunner
import org.junit.runner.RunWith
import org.scalatest.mock.MockitoSugar
import org.mockito.Mockito.{times, verify, when}
import org.mockito.{Matchers, Mockito}
import org.mockito.Matchers._
import com.twitter.finagle.Service
@RunWith(classOf[JUnitRunner])
class RefcountedServiceTest extends FunSuite with MockitoSugar {
class PoolServiceWrapperHelper {
val service = mock[Service[Any, Any]]
when(service.close(any)) thenReturn Future.Done
val promise = new Promise[Any]
when(service(Matchers.any)) thenReturn promise
val wrapper = Mockito.spy(new RefcountedService[Any, Any](service))
}
test("PoolServiceWrapper should call release() immediately when no requests have been made") {
val h = new PoolServiceWrapperHelper
import h._
verify(service, times(0)).close(any)
wrapper.close()
verify(service).close(any)
}
test("PoolServiceWrapper should call release() after pending request finishes") {
val h = new PoolServiceWrapperHelper
import h._
val f = wrapper(123)
assert(!f.isDefined)
verify(service)(123)
wrapper.close()
verify(service, times(0)).close(any)
promise() = Return(123)
verify(service).close(any)
assert(f.isDefined)
assert(Await.result(f) == 123)
}
}
示例2: ThriftServerFramedCodecTest
//设置package包名称以及导入依赖的类
package com.twitter.finagle.thrift
import com.twitter.finagle.Service
import com.twitter.finagle.tracing._
import com.twitter.finagle.util.ByteArrays
import com.twitter.util.Future
import org.apache.thrift.protocol.{TBinaryProtocol, TMessage, TMessageType}
import org.junit.runner.RunWith
import org.mockito.Matchers
import org.mockito.Mockito.when
import org.scalatest.FunSuite
import org.scalatest.junit.JUnitRunner
import org.scalatest.mock.MockitoSugar
@RunWith(classOf[JUnitRunner])
class ThriftServerFramedCodecTest extends FunSuite with MockitoSugar {
val protocolFactory = new TBinaryProtocol.Factory()
test("ThriftServerTracingFilter read header correctly") {
val traceId = TraceId(Some(SpanId(1L)), None, SpanId(2L), Some(true), Flags().setDebug)
val bufferingTracer = new BufferingTracer
Trace.letTracer(bufferingTracer) {
val filter = new TTwitterServerFilter("service", protocolFactory)
val upgradeMsg = new OutputBuffer(protocolFactory)
upgradeMsg().writeMessageBegin(new TMessage(ThriftTracing.CanTraceMethodName, TMessageType.CALL, 0))
val options = new thrift.ConnectionOptions
options.write(upgradeMsg())
upgradeMsg().writeMessageEnd()
val service = mock[Service[Array[Byte], Array[Byte]]]
when(service(Matchers.any[Array[Byte]])).thenReturn(Future(Array[Byte]()))
// now finagle knows we can handle the headers
filter(upgradeMsg.toArray, service)
// let's create a header
val header = new thrift.RequestHeader
header.setSpan_id(2L)
header.setTrace_id(1L)
header.setSampled(true)
header.setFlags(1L)
val ignoreMsg = new OutputBuffer(protocolFactory)
ignoreMsg().writeMessageBegin(new TMessage("ignoreme", TMessageType.CALL, 0))
new thrift.ConnectionOptions().write(ignoreMsg())
ignoreMsg().writeMessageEnd()
filter(ByteArrays.concat(OutputBuffer.messageToArray(header, protocolFactory), ignoreMsg.toArray), service)
bufferingTracer.iterator foreach { record =>
assert(record.traceId == traceId)
assert(record.traceId.flags == traceId.flags)
}
}
}
}
示例3: ClientIdRequiredFilterTest
//设置package包名称以及导入依赖的类
package com.twitter.finagle.thrift
import com.twitter.conversions.time._
import com.twitter.finagle.Service
import com.twitter.util.{Await, Future}
import org.junit.runner.RunWith
import org.mockito.Matchers
import org.mockito.Mockito.{times, verify, when}
import org.scalatest.FunSuite
import org.scalatest.junit.JUnitRunner
import org.scalatest.mock.MockitoSugar
@RunWith(classOf[JUnitRunner])
class ClientIdRequiredFilterTest extends FunSuite with MockitoSugar {
case class ClientIdRequiredFilterContext(underlying: Service[String,String]) {
lazy val service = new ClientIdRequiredFilter andThen underlying
}
val request = "request"
val response = Future.value("response")
val clientId = ClientId("test")
test("ClientIdRequiredFilter passes through when ClientId exists") {
val c = ClientIdRequiredFilterContext(mock[Service[String,String]])
import c._
when(underlying(request)).thenReturn(response)
clientId.asCurrent {
val result = service(request)
assert(Await.result(result, 10.seconds) == Await.result(response, 10.seconds))
result
}
}
test("ClientIdRequiredFilter throws NoClientIdSpecifiedException when ClientId does not exist") {
val c = ClientIdRequiredFilterContext(mock[Service[String,String]])
import c._
ClientId.let(None) {
intercept[NoClientIdSpecifiedException]{
Await.result(service(request), 10.seconds)
}
verify(underlying, times(0)).apply(Matchers.anyString())
}
}
}
示例4: SignalRefreshedTemplateSourceSpec
//设置package包名称以及导入依赖的类
package de.frosner.broccoli.templates
import de.frosner.broccoli.signal.SignalManager
import org.mockito.Mockito.{times, verify}
import org.mockito.{ArgumentCaptor, Matchers}
import org.specs2.mock.Mockito
import org.specs2.mutable.Specification
import sun.misc.{Signal, SignalHandler}
class SignalRefreshedTemplateSourceSpec extends Specification with Mockito {
"Receiving a SIGUSR2 signal" should {
"update the cache" in {
val signalManager = mock[SignalManager]
val testTemplateSource = mock[CachedTemplateSource]
val signalRefreshedTemplateSource = new SignalRefreshedTemplateSource(testTemplateSource, signalManager)
val handler = ArgumentCaptor.forClass(classOf[SignalHandler])
there was one(signalManager).register(Matchers.eq(new Signal("USR2")), handler.capture())
there was no(testTemplateSource).refresh()
there was no(testTemplateSource).loadTemplates()
signalRefreshedTemplateSource.loadTemplates()
there was no(testTemplateSource).refresh()
there was one(testTemplateSource).loadTemplates()
verify(testTemplateSource, times(1)).loadTemplates()
handler.getValue.handle(new Signal("USR2"))
there was one(testTemplateSource).refresh()
there was one(testTemplateSource).loadTemplates()
}
}
}
示例5: ClientBuilderSpec
//设置package包名称以及导入依赖的类
package com.twitter.finagle.builder
import com.twitter.finagle._
import com.twitter.finagle.integration.IntegrationBase
import com.twitter.util.{Promise, Return, Future}
import org.mockito.Matchers
import org.specs.SpecificationWithJUnit
import org.specs.mock.Mockito
class ClientBuilderSpec extends SpecificationWithJUnit with IntegrationBase with Mockito {
"ClientBuilder" should {
"invoke prepareConnFactory on connection" in {
val preparedFactory = mock[ServiceFactory[String, String]]
val preparedServicePromise = new Promise[Service[String, String]]
preparedFactory() returns preparedServicePromise
preparedFactory.close(any) returns Future.Done
preparedFactory.map(Matchers.any()) returns
preparedFactory.asInstanceOf[ServiceFactory[Any, Nothing]]
val m = new MockChannel
m.codec.prepareConnFactory(any) returns preparedFactory
// Client
val client = m.build()
val requestFuture = client("123")
there was one(m.codec).prepareConnFactory(any)
there was one(preparedFactory)()
requestFuture.isDefined must beFalse
val service = mock[Service[String, String]]
service("123") returns Future.value("321")
service.close(any) returns Future.Done
preparedServicePromise() = Return(service)
there was one(service)("123")
requestFuture.poll must beSome(Return("321"))
}
}
}
示例6: CloseOnReleaseServiceSpec
//设置package包名称以及导入依赖的类
package com.twitter.finagle.service
import com.twitter.finagle.{Service, WriteException}
import com.twitter.util.{Future, Promise}
import org.mockito.Matchers
import org.specs.SpecificationWithJUnit
import org.specs.mock.Mockito
class CloseOnReleaseServiceSpec extends SpecificationWithJUnit with Mockito {
"CloseOnReleaseService" should {
val service = mock[Service[Any, Any]]
service.close(any) returns Future.Done
val promise = new Promise[Any]
service(Matchers.any) returns promise
val wrapper = new CloseOnReleaseService(service)
"only call release on the underlying service once" in {
wrapper.close()
there was one(service).close(any)
wrapper.close()
there was one(service).close(any)
service.isAvailable must beFalse
}
"throw a write exception if we attempt to use a released service" in {
wrapper.close()
wrapper(132)() must throwA[WriteException]
}
}
}
示例7: RateLimitingFilterSpec
//设置package包名称以及导入依赖的类
package com.twitter.finagle.service
import org.specs.SpecificationWithJUnit
import org.specs.mock.Mockito
import com.twitter.finagle.Service
import com.twitter.util.TimeConversions._
import org.mockito.Matchers
import com.twitter.util.{Time, Future}
class RateLimitingFilterSpec extends SpecificationWithJUnit with Mockito {
"RateLimitingFilter" should {
def categorize(i: Int) = (i%5).toString
val strategy = new LocalRateLimitingStrategy[Int](categorize, 1.second, 5)
val filter = new RateLimitingFilter[Int, Int](strategy)
val service = mock[Service[Int, Int]]
service.close(any) returns Future.Done
service(Matchers.anyInt) returns Future.value(1)
val rateLimitedService = filter andThen service
"Execute requests below rate limit" in {
var t = Time.now
Time.withTimeFunction(t) { _ =>
(1 to 5) foreach { _ =>
rateLimitedService(1)() mustBe 1
t += 100.milliseconds
}
}
}
"Refuse request if rate is above limit" in {
var t = Time.now
Time.withTimeFunction(t) { _ =>
(1 to 5) foreach { _ =>
rateLimitedService(1)() mustBe 1
t += 100.milliseconds
}
rateLimitedService(1)() must throwA[Exception]
}
}
"Execute different categories of requests and keep a window per category" in {
var t = Time.now
Time.withTimeFunction(t) { _ =>
(1 to 5) foreach { _ =>
(1 to 5) foreach { i => rateLimitedService(i)() mustBe 1 }
t += 100.milliseconds
}
}
}
}
}
示例8: RefcountedServiceSpec
//设置package包名称以及导入依赖的类
package com.twitter.finagle.service
import com.twitter.finagle.Service
import com.twitter.util.{Future, Promise, Return}
import org.mockito.Matchers
import org.specs.SpecificationWithJUnit
import org.specs.mock.Mockito
class RefcountedServiceSpec extends SpecificationWithJUnit with Mockito {
"PoolServiceWrapper" should {
val service = mock[Service[Any, Any]]
service.close(any) returns Future.Done
val promise = new Promise[Any]
service(Matchers.any) returns promise
val wrapper = spy(new RefcountedService[Any, Any](service))
"call release() immediately when no requests have been made" in {
there was no(service).close(any)
wrapper.close()
there was one(service).close(any)
}
"call release() after pending request finishes" in {
val f = wrapper(123)
f.isDefined must beFalse
there was one(service)(123)
wrapper.close()
there was no(service).close(any)
promise() = Return(123)
there was one(service).close(any)
f.isDefined must beTrue
f() must be_==(123)
}
}
}
示例9: EncryptedIndexStoreTest
//设置package包名称以及导入依赖的类
package org.elasticsearch.index.store
import org.elasticsearch.client.Client
import org.elasticsearch.common.settings.Settings
import org.elasticsearch.common.unit.ByteSizeValue
import org.elasticsearch.env.NodeEnvironment
import org.elasticsearch.index.settings.IndexSettingsService
import org.elasticsearch.index.{Index, IndexService}
import org.elasticsearch.indices.store.IndicesStore
import org.scalatest.FlatSpec
import org.scalatest.mockito.MockitoSugar
import org.mockito.Mockito.when
import org.mockito.Matchers
class EncryptedIndexStoreTest extends FlatSpec with org.scalatest.Matchers with MockitoSugar {
behavior of "#shardDirectory"
it should "return EncryptedDirectoryService" in {
val settings = mock[Settings]
when(settings.getComponentSettings(Matchers.any())).thenReturn(null)
when(settings.get(Matchers.eq("index.store.throttle.type"), Matchers.anyString())).thenReturn("all")
when(settings.getAsBytesSize(Matchers.eq("index.store.throttle.max_bytes_per_sec"), Matchers.any())).thenReturn(new ByteSizeValue(1000))
val nodeEnv = mock[NodeEnvironment]
when(nodeEnv.hasNodeFile).thenReturn(false)
val indexService = mock[IndexService]
val indexSettingsService = mock[IndexSettingsService]
when(indexService.settingsService).thenReturn(indexSettingsService)
val eis = new EncryptedIndexStore(mock[Index], settings, indexService, mock[IndicesStore], nodeEnv, mock[Client])
eis.shardDirectory() shouldBe classOf[EncryptedDirectoryService]
}
}