当前位置: 首页>>代码示例>>Scala>>正文


Scala ArgumentCaptor类代码示例

本文整理汇总了Scala中org.mockito.ArgumentCaptor的典型用法代码示例。如果您正苦于以下问题:Scala ArgumentCaptor类的具体用法?Scala ArgumentCaptor怎么用?Scala ArgumentCaptor使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了ArgumentCaptor类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: ThriftRichClientTest

//设置package包名称以及导入依赖的类
package com.twitter.finagle.thrift

import com.twitter.finagle._
import com.twitter.finagle.stats.StatsReceiver
import org.apache.thrift.protocol.TProtocolFactory
import org.junit.runner.RunWith
import org.mockito.ArgumentCaptor
import org.mockito.Mockito._
import org.mockito.Matchers._
import org.scalatest.{OneInstancePerTest, FunSuite}
import org.scalatest.junit.JUnitRunner
import org.scalatest.mock.MockitoSugar


@RunWith(classOf[JUnitRunner])
class ThriftRichClientTest extends FunSuite with MockitoSugar with OneInstancePerTest {
  object ThriftRichClientMock extends Client[ThriftClientRequest, Array[Byte]] with ThriftRichClient {
    override val protocolFactory: TProtocolFactory = Protocols.binaryFactory()
    override val defaultClientName = "mock_client"

    protected def params: Stack.Params = Stack.Params.empty

    def newService(
      dest: Name,
      label: String
    ): Service[ThriftClientRequest, Array[Byte]] =
      mock[Service[ThriftClientRequest, Array[Byte]]]

    def newClient(
      dest: Name,
      label: String
    ): ServiceFactory[ThriftClientRequest, Array[Byte]] =
      mock[ServiceFactory[ThriftClientRequest, Array[Byte]]]
  }


  test("ThriftRichClientTest newServiceIface takes dest String and stats scoping label arguments") {
    val captor = ArgumentCaptor.forClass(classOf[StatsReceiver])
    val mockBuilder = mock[ServiceIfaceBuilder[String]]
    doReturn("mockServiceIface").when(mockBuilder).newServiceIface(any(), any(), captor.capture())

    val client = spy(ThriftRichClientMock)
    client.newServiceIface("/s/tweetypie/tweetypie", "tweetypie_client")(builder = mockBuilder)

    assert(captor.getValue.toString == "NullStatsReceiver/clnt/tweetypie_client")
    verify(client).newService("/s/tweetypie/tweetypie", "tweetypie_client")
  }

  test("ThriftRichClientTest newServiceIface takes dest Name and stats scoping label arguments") {
    val captor = ArgumentCaptor.forClass(classOf[StatsReceiver])
    val mockBuilder = mock[ServiceIfaceBuilder[String]]
    doReturn("mockServiceIface").when(mockBuilder).newServiceIface(any(), any(), captor.capture())

    val name = Name.empty
    val client = spy(ThriftRichClientMock)
    client.newServiceIface(name, "tweetypie_client")(builder = mockBuilder)

    assert(captor.getValue.toString == "NullStatsReceiver/clnt/tweetypie_client")
    verify(client).newService(name, "tweetypie_client")
  }
} 
开发者ID:wenkeyang,项目名称:finagle,代码行数:62,代码来源:ThriftRichClientTest.scala

示例2: any

//设置package包名称以及导入依赖的类
package ru.finagram.test

import org.mockito.verification.VerificationWithTimeout
import org.mockito.{ ArgumentCaptor, Mockito }

import scala.concurrent.duration._
import scala.reflect.Manifest

trait MockitoSugar extends org.scalatest.mockito.MockitoSugar {

  def any[T <: Any](implicit manifest: Manifest[T]): T = {
    org.mockito.Matchers.any(manifest.runtimeClass.asInstanceOf[Class[T]])
  }

  def argumentCaptor[T <: Any](implicit manifest: Manifest[T]): ArgumentCaptor[T] = {
    ArgumentCaptor.forClass(manifest.runtimeClass.asInstanceOf[Class[T]])
  }

  def timeout(duration: Duration): VerificationWithTimeout = {
    Mockito.timeout(duration.toMillis.toInt)
  }
} 
开发者ID:finagram,项目名称:finagram,代码行数:23,代码来源:MockitoSugar.scala

示例3: 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()
    }
  }
} 
开发者ID:FRosner,项目名称:cluster-broccoli,代码行数:33,代码来源:SignalRefreshedTemplateSourceSpec.scala

示例4: ScanResultIteratorTest

//设置package包名称以及导入依赖的类
package com.mobilerq.awsutil.dynamodb

import java.util.Collections

import com.amazonaws.services.dynamodbv2.AmazonDynamoDB
import com.amazonaws.services.dynamodbv2.model.{AttributeValue, ScanRequest, ScanResult}
import org.junit.Assert._
import org.junit.Test
import org.mockito.ArgumentCaptor
import org.mockito.Mockito._
import org.mockito.ArgumentMatchers._

import scala.collection.JavaConverters._

class ScanResultIteratorTest {
  implicit val client = mock(classOf[AmazonDynamoDB])
  val requestCaptor: ArgumentCaptor[ScanRequest] = ArgumentCaptor.forClass(classOf[ScanRequest])

  @Test
  def testIteration(): Unit = {
    val items = Seq(
      Map("item" ? new AttributeValue("1")),
      Map("item" ? new AttributeValue("2")),
      Map("item" ? new AttributeValue("3"))
    )
    when(client.scan(any(classOf[ScanRequest])))
      .thenReturn(new ScanResult()
        .withLastEvaluatedKey(items(1).asJava)
        .withItems(Seq(items(0).asJava, items(1).asJava).asJavaCollection)
        .withCount(2))
      .thenReturn(new ScanResult()
        .withLastEvaluatedKey(items(2).asJava)
        .withItems(Seq(items(2).asJava).asJavaCollection)
        .withCount(1))
      .thenReturn(new ScanResult()
        .withItems(Collections.emptyList[java.util.Map[String, AttributeValue]]())
        .withCount(0))
      .thenThrow(new RuntimeException("ouch"))
    val result = new ScanResultIterator(new ScanRequest).flatMap(_.getItems.asScala).map(_.asScala("item").getS).toList
    assertEquals(List("1", "2", "3"), result)
  }

} 
开发者ID:mobilerq,项目名称:mrq-aws-util,代码行数:44,代码来源:ScanResultIteratorTest.scala

示例5: EndQueueJobSpec

//设置package包名称以及导入依赖的类
package articlestreamer.aggregator.kafka.scheduled

import articlestreamer.shared.BaseSpec
import articlestreamer.shared.kafka.KafkaProducerWrapper
import org.apache.kafka.clients.producer.ProducerRecord
import org.mockito.ArgumentCaptor
import org.mockito.ArgumentMatchers._
import org.mockito.Mockito._
import org.quartz.{JobDataMap, JobDetail, JobExecutionContext}

class EndQueueJobSpec extends BaseSpec {

  "Executing job" should "send an end of queue message" in {
    val job = new EndQueueJob()

    val captor: ArgumentCaptor[ProducerRecord[String, String]] = ArgumentCaptor.forClass(classOf[ProducerRecord[String, String]])

    val producer = mock(classOf[KafkaProducerWrapper])
    when(producer.send(captor.capture())).thenReturn(null)
    val data = new JobDataMap()
    data.put("producer", producer)
    data.put("topic", "topic1")
    val jobDetails = mock(classOf[JobDetail])
    when(jobDetails.getJobDataMap).thenReturn(data)
    val context = mock(classOf[JobExecutionContext])
    when(context.getJobDetail).thenReturn(jobDetails)

    job.execute(context)

    verify(producer, times(1)).send(any())
    captor.getValue.key() shouldBe "endOfQueue"
    captor.getValue.value() shouldBe ""
  }

} 
开发者ID:firens,项目名称:article-streamer,代码行数:36,代码来源:EndQueueJobSpec.scala

示例6: TimerSpec

//设置package包名称以及导入依赖的类
package com.twitter.finagle.util

import com.twitter.conversions.time._
import com.twitter.util.TimerTask
import java.util.Collections
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger
import org.jboss.netty.{util => nu}
import org.mockito.ArgumentCaptor
import org.specs.SpecificationWithJUnit
import org.specs.mock.Mockito

class TimerSpec extends SpecificationWithJUnit with Mockito {
  "TimerFromNettyTimer" should {
    val timer = mock[nu.Timer]
    val nstop = new AtomicInteger(0)
    @volatile var running = true
    timer.stop() answers { args =>
      running = false
      nstop.incrementAndGet()
      Collections.emptySet()
    }

    "Support cancelling recurring tasks" in {
      val t = new TimerFromNettyTimer(timer)

      val taskCaptor = ArgumentCaptor.forClass(classOf[nu.TimerTask])
      val firstTimeout = mock[nu.Timeout]
      firstTimeout.isCancelled returns false
      timer.newTimeout(taskCaptor.capture(), any, any) returns firstTimeout

      var task: TimerTask = null
      task = t.schedule(1.second) { task.cancel() }

      taskCaptor.getValue.run(firstTimeout)

      there was atMostOne(timer).newTimeout(any, any, any)
    }
  }
} 
开发者ID:deenar,项目名称:fintest,代码行数:41,代码来源:TimerSpec.scala

示例7: setUpResponse

//设置package包名称以及导入依赖的类
package utils

import logging.{WSMethods, WSRequestWrapper}
import org.mockito.ArgumentCaptor
import org.mockito.Matchers.{eq => mockitoEq, _}
import org.mockito.Mockito._
import play.api.http.{ContentTypeOf, Writeable}
import play.api.libs.ws.WSResponse

import scala.concurrent.Future

trait WsClient extends Mocking {

  val wsClient = mock[WSMethods]
  val mockRequestHolder = mock[WSRequestWrapper]
  val mockResponse = mock[WSResponse]
  private val bodyCaptor = ArgumentCaptor.forClass(classOf[String])

  def setUpResponse(url: String, statusCode: Int, responseBody: String = "") = {
    setUpRequestHolder(url)
    setUpResponseHolder(statusCode, responseBody)
  }
  
  def setUpException(url: String) = {
    setUpRequestHolder(url)
    when(mockRequestHolder.post(any[String])(any[Writeable[String]], any[ContentTypeOf[String]])).thenReturn(Future.successful(mockResponse))
    when(mockResponse.status).thenThrow(new RuntimeException("Boom"))
  }

  def requestBody() = {
    verify(mockRequestHolder, timeout(500)).post(bodyCaptor.capture())(any[Writeable[String]], any[ContentTypeOf[String]])
    bodyCaptor.getValue
  }

  private def setUpRequestHolder(url: String) = {
    when(mockRequestHolder.withHeaders(any[(String, String)])).thenReturn(mockRequestHolder)
    when(mockRequestHolder.withQueryString(any[(String, String)])).thenReturn(mockRequestHolder)
    when(wsClient.url(mockitoEq(url))).thenReturn(mockRequestHolder)
  }

  private def setUpResponseHolder(statusCode: Int, responseBody: String) = {
    when(mockResponse.status).thenReturn(statusCode)
    when(mockResponse.body).thenReturn(responseBody)
    when(mockRequestHolder.post(any[String])(any[Writeable[String]], any[ContentTypeOf[String]])).thenReturn(Future.successful(mockResponse))
  }
} 
开发者ID:UKHomeOffice,项目名称:submission-service,代码行数:47,代码来源:WsClient.scala

示例8: LoopClockTest

//设置package包名称以及导入依赖的类
package hcube.scheduler

import hcube.scheduler.utils.TimeUtil.SleepFn
import org.mockito.{ArgumentCaptor, Mockito}
import org.specs2.mock.{Mockito => MockitoSpecs2}
import org.specs2.mutable.Specification

import scala.collection.JavaConversions._
import scala.collection.mutable
import scala.concurrent.duration.Duration
import scala.concurrent.{Await, ExecutionContext}

class LoopClockTest extends Specification with MockitoSpecs2 {

  implicit val ec: ExecutionContext = ExecutionContext.Implicits.global


  "generate expected tick intervals" >> {
    val tickReceiver = mock[TickReceiver]
    val timeQueue = mutable.Queue(1000L, 1300L, 2040L, 3020L, 4200L, 7200L, 8000L)
    val timeFn = () => {
      timeQueue.dequeue()
    }
    val sleep = mock[SleepFn]

    val clock = new LoopClock(
      tickReceiver,
      tickPeriod = 1000,
      tolerance = 50,
      continueOnInterrupt = false,
      currentTimeMillis = timeFn,
      sleep = sleep,
      stopTime = Some(8000)
    )

    Await.result(clock(), Duration.Inf)

    val t0Arg = ArgumentCaptor.forClass(classOf[Long])
    val t1Arg = ArgumentCaptor.forClass(classOf[Long])
    Mockito.verify(tickReceiver, Mockito.times(6))
      .tick(t0Arg.capture(), t1Arg.capture())

    val sleepArg = ArgumentCaptor.forClass(classOf[Long])
    Mockito.verify(sleep, Mockito.times(4))
      .apply(sleepArg.capture())

    List(1000L, 2000L, 3000L, 4000L, 5000L, 6000L) must_== t0Arg.getAllValues.toList
    List(2000L, 3000L, 4000L, 5000L, 6000L, 8000L) must_== t1Arg.getAllValues.toList

    List(700, 960, 980, 800L) must_== sleepArg.getAllValues.toList
  }

} 
开发者ID:hcube-dev,项目名称:hcube-scheduler,代码行数:54,代码来源:LoopClockTest.scala


注:本文中的org.mockito.ArgumentCaptor类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。