本文整理汇总了Scala中org.mockito.Mockito.times类的典型用法代码示例。如果您正苦于以下问题:Scala times类的具体用法?Scala times怎么用?Scala times使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了times类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: OptionallyServableFilterTest
//设置package包名称以及导入依赖的类
package com.twitter.finagle.service
import org.junit.runner.RunWith
import org.scalatest.FunSuite
import org.scalatest.junit.JUnitRunner
import org.scalatest.mock.MockitoSugar
import com.twitter.finagle.{NotServableException, Service}
import org.mockito.Mockito.{times, verify, when}
import org.mockito.Matchers._
import com.twitter.util.{Await, Future}
@RunWith(classOf[JUnitRunner])
class OptionallyServableFilterTest extends FunSuite with MockitoSugar {
class OptionnallyServableFilterHelper {
val underlying = mock[Service[String, String]]
when(underlying.close(any)) thenReturn Future.Done
val fn = mock[String => Future[Boolean]]
val service = new OptionallyServableFilter(fn) andThen underlying
val request = "request"
val response = Future.value("response")
}
test("OptionallyServableFilter should passes through when fn returns true") {
val h = new OptionnallyServableFilterHelper
import h._
when(fn.apply(request)) thenReturn Future.value(true)
when(underlying(request)) thenReturn response
assert(Await.result(service(request)) == Await.result(response))
verify(fn).apply(request)
}
test("OptionallyServableFilter should throws NotServableException when fn returns false") {
val h = new OptionnallyServableFilterHelper
import h._
when(fn.apply(request)) thenReturn Future.value(false)
intercept[NotServableException] {
Await.result(service(request))
}
verify(underlying, times(0)).apply(any[String])
verify(fn).apply(request)
}
}
示例3: CloseOnReleaseServiceTest
//设置package包名称以及导入依赖的类
package com.twitter.finagle.service
import org.junit.runner.RunWith
import org.mockito.Mockito.{verify, when, times}
import org.mockito.Matchers._
import org.scalatest.FunSuite
import org.scalatest.junit.JUnitRunner
import org.scalatest.mock.MockitoSugar
import com.twitter.finagle.{WriteException, Service, Status}
import com.twitter.util.{Await, Promise, Future}
@RunWith(classOf[JUnitRunner])
class CloseOnReleaseServiceTest extends FunSuite with MockitoSugar {
class Helper {
val service = mock[Service[Any, Any]]
when(service.close(any)) thenReturn Future.Done
val promise = new Promise[Any]
when(service(any)) thenReturn promise
when(service.status) thenReturn Status.Open
val wrapper = new CloseOnReleaseService(service)
}
test("only call release on the underlying service once") {
val h = new Helper
import h._
assert(wrapper.isAvailable)
verify(service, times(1)).status
wrapper.close()
verify(service, times(1)).close(any)
wrapper.close()
verify(service, times(1)).close(any)
assert(!wrapper.isAvailable)
verify(service, times(1)).status
}
test("throw a write exception if we attempt to use a released service") {
val h = new Helper
import h._
wrapper.close()
intercept[WriteException] {
Await.result(wrapper(132))
}
}
}
示例4: GenerationalRandomTest
//设置package包名称以及导入依赖的类
package com.twitter.finagle.mux.lease.exp
import org.junit.runner.RunWith
import org.mockito.Mockito.{when, verify, times}
import org.scalatest.FunSuite
import org.scalatest.junit.JUnitRunner
import org.scalatest.mock.MockitoSugar
import scala.util.Random
@RunWith(classOf[JUnitRunner])
class GenerationalRandomTest extends FunSuite with MockitoSugar {
test("GenerationalRandom stays the same intragenerationally") {
val nfo = mock[JvmInfo]
when(nfo.generation()).thenReturn(0)
val rnd = new Random(12334)
val gen = new GenerationalRandom(nfo, rnd)
verify(nfo).generation()
val x = gen()
verify(nfo, times(2)).generation()
assert(gen() == x)
verify(nfo, times(3)).generation()
}
test("GenerationalRandom changes intergenerationally") {
val nfo = mock[JvmInfo]
when(nfo.generation()).thenReturn(0)
val rnd = new Random(12334)
val gen = new GenerationalRandom(nfo, rnd)
verify(nfo).generation()
val x = gen()
verify(nfo, times(2)).generation()
assert(gen() == x)
verify(nfo, times(3)).generation()
when(nfo.generation()).thenReturn(1)
assert(gen() != x)
verify(nfo, times(5)).generation()
}
}
示例5: 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())
}
}
}
示例6: RubyMemCacheClientTest
//设置package包名称以及导入依赖的类
package com.twitter.finagle.memcached.unit
import com.twitter.finagle.memcached._
import org.junit.runner.RunWith
import org.mockito.Mockito.{times, verify}
import org.scalatest.FunSuite
import org.scalatest.junit.JUnitRunner
import org.scalatest.mock.MockitoSugar
@RunWith(classOf[JUnitRunner])
class RubyMemCacheClientTest extends FunSuite with MockitoSugar {
class Context {
val client1 = mock[Client]
val client2 = mock[Client]
val client3 = mock[Client]
val rubyMemCacheClient = new RubyMemCacheClient(Seq(client1, client2, client3))
}
test("pick the correct node") {
val context = new Context
import context._
assert(rubyMemCacheClient.clientOf("apple") == (client1))
assert(rubyMemCacheClient.clientOf("banana") == (client2))
assert(rubyMemCacheClient.clientOf("cow") == (client1))
assert(rubyMemCacheClient.clientOf("dog") == (client1))
assert(rubyMemCacheClient.clientOf("elephant") == (client3))
}
test("release") {
val context = new Context
import context._
rubyMemCacheClient.release()
verify(client1, times(1)).release()
verify(client2, times(1)).release()
verify(client3, times(1)).release()
}
}
示例7: 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()
}
}
}
示例8: StackDescribeTest
//设置package包名称以及导入依赖的类
package uk.co.telegraph.plugin.pipeline.tasks
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.{eq => mEq}
import org.mockito.Mockito
import org.mockito.Mockito.{times, verify}
import org.scalatest.junit.JUnitRunner
import org.scalatest.{BeforeAndAfter, FunSpec, Matchers}
import sbt.Logger
import uk.co.telegraph.cloud._
import uk.co.telegraph.plugin.pipeline.StackAuth
import StackDescribeTest._
@RunWith(classOf[JUnitRunner])
class StackDescribeTest extends FunSpec with BeforeAndAfter with Matchers{
val SampleInvalidStackName = "invalid-test-stack"
before{
Mockito.reset(MockInterpreter)
}
describe("Given the 'StackDescribe' operation, "){
it("I should be able to get a stack name if the stack exists"){
Mockito.when(MockInterpreter.apply( mEq(Describe(SampleStackName)) ))
.thenReturn(Some(SampleStackInfo))
val res = StackDescribeMocked(
SampleStackName,
SampleStackRegion,
SampleStackAuth
)(SampleEnvironment, SampleLogger)
res shouldBe Some(SampleStackInfo)
verify(MockInterpreter, times(1)).apply( mEq(Describe(SampleStackName)) )
}
it("I should get nothing with an invalid stack"){
Mockito.when(MockInterpreter.apply( mEq(Describe(SampleInvalidStackName)) ))
.thenReturn(None)
val res = StackDescribeMocked(
SampleInvalidStackName,
SampleStackRegion,
SampleStackAuth
)(SampleEnvironment, SampleLogger)
res shouldBe None
verify(MockInterpreter, times(1)).apply( mEq(Describe(SampleInvalidStackName)) )
}
}
}
object StackDescribeTest {
object StackDescribeMocked extends StackDescribe{
override def interpreter(region: StackRegion, auth: StackAuth)(implicit logger: Logger) = MockInterpreter
}
}
示例9: AESIndexOutputTest
//设置package包名称以及导入依赖的类
package org.elasticsearch.index.store
import java.io.File
import org.apache.lucene.util.AESWriter
import org.mockito.Matchers.{any, anyInt}
import org.mockito.Mockito.{times, verify}
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{FlatSpec, Matchers}
class AESIndexOutputTest extends FlatSpec with Matchers with MockitoSugar {
behavior of "#write"
it should "write data into writer" in {
val dir = mock[File]
val writer = mock[AESWriter]
var counter = 0
def closeHandler(name: String) = { counter += 1 }
def createAESWriter(dir: File, name: String, pageSize: Int) = { writer }
val output = new AESIndexOutput(dir, "test", 100, closeHandler, createAESWriter)
val bytes = (1 to 10000).map(_.toByte).toArray[Byte] // We need more data than 8192 byte write buffer
output.writeBytes(bytes, 0, 9000)
verify(writer, times(2)).write(any[Array[Byte]], anyInt, anyInt)
}
behavior of "#flush"
it should "flush the writer" in {
val dir = mock[File]
val writer = mock[AESWriter]
var counter = 0
def closeHandler(name: String) = { counter += 1 }
def createAESWriter(dir: File, name: String, pageSize: Int) = { writer }
val output = new AESIndexOutput(dir, "test", 100, closeHandler, createAESWriter)
output.flush()
verify(writer, times(1)).flush()
}
behavior of "#close"
it should "call the handler" in {
val dir = mock[File]
val writer = mock[AESWriter]
var counter = 0
def closeHandler(name: String) = { counter += 1 }
def createAESWriter(dir: File, name: String, pageSize: Int) = { writer }
val output = new AESIndexOutput(dir, "test", 100, closeHandler, createAESWriter)
counter shouldBe 0
output.close()
counter shouldBe 1
}
}
示例10: EncryptedIndexStoreModuleTest
//设置package包名称以及导入依赖的类
package org.elasticsearch.index.store
import org.scalatest.{FlatSpec, Matchers}
import org.scalatest.mockito.MockitoSugar
import org.mockito.Mockito.{when,verify,times}
import org.elasticsearch.common.inject.Binder
import org.elasticsearch.common.inject.binder.{AnnotatedBindingBuilder, ScopedBindingBuilder}
class EncryptedIndexStoreModuleTest extends FlatSpec with Matchers with MockitoSugar {
behavior of "#configure"
it should "not fail" in {
val module = new EncryptedIndexStoreModule()
val binder = mock[Binder]
val annotatedBinder = mock[AnnotatedBindingBuilder[IndexStore]]
val scopedBuilder = mock[ScopedBindingBuilder]
when(binder.bind(classOf[IndexStore])).thenReturn(annotatedBinder)
when(annotatedBinder.to(classOf[EncryptedIndexStore])).thenReturn(scopedBuilder)
module.configure(binder)
verify(scopedBuilder, times(1)).asEagerSingleton()
}
}