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


Scala Answer类代码示例

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


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

示例1: equalTo

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

import org.mockito.invocation.InvocationOnMock
import org.mockito.stubbing.{ Answer, OngoingStubbing }
import org.mockito.verification.VerificationMode
import org.mockito.{ Mockito => M }
import org.scalatest.mockito.MockitoSugar


trait Mockito extends MockitoSugar {

  def equalTo[T](t: T) = org.mockito.Matchers.eq(t)
  def eq[T](t: T) = org.mockito.Matchers.eq(t)
  def any[T] = org.mockito.Matchers.any[T]
  def anyBoolean = org.mockito.Matchers.anyBoolean
  def anyString = org.mockito.Matchers.anyString
  def same[T](value: T) = org.mockito.Matchers.same(value)
  def verify[T](t: T, mode: VerificationMode = times(1)) = M.verify(t, mode)
  def times(num: Int) = M.times(num)
  def timeout(millis: Int) = M.timeout(millis.toLong)
  def atLeastOnce = M.atLeastOnce()
  def once = M.times(1)
  def atLeast(num: Int) = M.atLeast(num)
  def atMost(num: Int) = M.atMost(num)
  def never = M.never()

  def inOrder(mocks: AnyRef*) = M.inOrder(mocks: _*)

  def noMoreInteractions(mocks: AnyRef*): Unit = {
    M.verifyNoMoreInteractions(mocks: _*)
  }

  def reset(mocks: AnyRef*): Unit = {
    M.reset(mocks: _*)
  }

  class MockAnswer[T](function: Array[AnyRef] => T) extends Answer[T] {
    def answer(invocation: InvocationOnMock): T = {
      function(invocation.getArguments)
    }
  }

  implicit class Stubbed[T](c: => T) {
    def returns(t: T, t2: T*): OngoingStubbing[T] = {
      if (t2.isEmpty) M.when(c).thenReturn(t)
      else t2.foldLeft (M.when(c).thenReturn(t)) { (res, cur) => res.thenReturn(cur) }
    }
    def answers(function: Array[AnyRef] => T) = M.when(c).thenAnswer(new MockAnswer(function))
    def throws[E <: Throwable](e: E*): OngoingStubbing[T] = {
      if (e.isEmpty) throw new java.lang.IllegalArgumentException("The parameter passed to throws must not be empty")
      e.drop(1).foldLeft(M.when(c).thenThrow(e.head)) { (res, cur) => res.thenThrow(cur) }
    }
  }
} 
开发者ID:xiaozai512,项目名称:marathon,代码行数:55,代码来源:Mockito.scala

示例2: mockito

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

import org.mockito.Mockito.{mock => mockitoMock}
import reflect.Manifest
import org.mockito.stubbing.Answer
import org.mockito.MockSettings

trait MockitoSugar {

  def mockito[T <: AnyRef](implicit manifest: Manifest[T]): T = {
    mockitoMock(manifest.erasure.asInstanceOf[Class[T]])
  }

  def mockito[T <: AnyRef](defaultAnswer: Answer[_])(implicit manifest: Manifest[T]): T = {
    mockitoMock(manifest.erasure.asInstanceOf[Class[T]], defaultAnswer)
  }

  def mockito[T <: AnyRef](mockSettings: MockSettings)(implicit manifest: Manifest[T]): T = {
    mockitoMock(manifest.erasure.asInstanceOf[Class[T]], mockSettings)
  }

  def mockito[T <: AnyRef](name: String)(implicit manifest: Manifest[T]): T = {
    mockitoMock(manifest.erasure.asInstanceOf[Class[T]], name)
  }
} 
开发者ID:DmytroOrlov,项目名称:devgym,代码行数:26,代码来源:MockitoSugar.scala

示例3: meq

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


import org.mockito.Mockito.{mock => mockitoMock}
import org.mockito.ArgumentMatchers.{eq => mockitoEq}
import reflect.ClassTag
import org.mockito.stubbing.Answer
import org.mockito.MockSettings

trait MockitoSuper
{
  def meq[T](value: T): T = mockitoEq(value)
  def smock[T <: AnyRef](implicit classTag: ClassTag[T]): T = {
    mockitoMock(classTag.runtimeClass.asInstanceOf[Class[T]])
  }
  def smock[T <: AnyRef](defaultAnswer: Answer[_])(implicit classTag: ClassTag[T]): T = {
    mockitoMock(classTag.runtimeClass.asInstanceOf[Class[T]], defaultAnswer)
  }
  def smock[T <: AnyRef](mockSettings: MockSettings)(implicit classTag: ClassTag[T]): T = {
    mockitoMock(classTag.runtimeClass.asInstanceOf[Class[T]], mockSettings)
  }
  def smock[T <: AnyRef](name: String)(implicit classTag: ClassTag[T]): T = {
    mockitoMock(classTag.runtimeClass.asInstanceOf[Class[T]], name)
  }
} 
开发者ID:apodemakeles,项目名称:webmonitor,代码行数:26,代码来源:MockitoSuper.scala

示例4: MockOrientDBDocument

//设置package包名称以及导入依赖的类
package org.apache.spark.orientdb.documents

import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx
import com.orientechnologies.orient.core.record.impl.ODocument
import org.apache.spark.orientdb.documents.Parameters.MergedParameters
import org.apache.spark.sql.types.StructType
import org.mockito.Matchers._
import org.mockito.Mockito._
import org.mockito.invocation.InvocationOnMock
import org.mockito.stubbing.Answer

class MockOrientDBDocument(existingTablesAndSchemas: Map[String, StructType],
                           oDocuments: List[ODocument]) {
  val documentWrapper: OrientDBDocumentWrapper = spy(new OrientDBDocumentWrapper())

  doAnswer(new Answer[ODatabaseDocumentTx] {
    override def answer(invocationOnMock: InvocationOnMock): ODatabaseDocumentTx = {
      mock(classOf[ODatabaseDocumentTx], RETURNS_SMART_NULLS)
    }
  }).when(documentWrapper).getConnection(any(classOf[MergedParameters]))

  doAnswer(new Answer[Boolean] {
    override def answer(invocationOnMock: InvocationOnMock): Boolean = {
      existingTablesAndSchemas.contains(invocationOnMock.getArguments()(1).asInstanceOf[String])
    }
  }).when(documentWrapper).doesClassExists(any(classOf[String]))

  doAnswer(new Answer[Boolean] {
    override def answer(invocationOnMock: InvocationOnMock): Boolean = {
      true
    }
  }).when(documentWrapper).create(any(classOf[String]), any(classOf[String]),
    any(classOf[ODocument]))

  doAnswer(new Answer[List[ODocument]] {
    override def answer(invocationOnMock: InvocationOnMock): List[ODocument] = {
      oDocuments
    }
  }).when(documentWrapper).read(any(classOf[String]), any(classOf[String]), any(classOf[Array[String]]),
    any(classOf[String]), any(classOf[String]))

  doAnswer(new Answer[Boolean] {
    override def answer(invocationOnMock: InvocationOnMock): Boolean = {
      true
    }
  }).when(documentWrapper).delete(any(classOf[String]), any(classOf[String]),
    any(classOf[Map[String, Tuple2[String, String]]]))

  doAnswer(new Answer[StructType] {
    override def answer(invocationOnMock: InvocationOnMock): StructType = {
      existingTablesAndSchemas.get(invocationOnMock.getArguments()(1).asInstanceOf[String]).get
    }
  }).when(documentWrapper).resolveTable(any(classOf[String]), any(classOf[String]))

  doAnswer(new Answer[List[ODocument]] {
    override def answer(invocationOnMock: InvocationOnMock): List[ODocument] = {
      oDocuments
    }
  }).when(documentWrapper).genericQuery(any(classOf[String]))
} 
开发者ID:orientechnologies,项目名称:spark-orientdb,代码行数:61,代码来源:MockOrientDBDocument.scala

示例5: KafkaEventSourceTest

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

import java.util
import java.util.Collections

import kpi.twitter.analysis.utils.{PredictedStatus, SentimentLabel, TweetSerDe}
import org.apache.kafka.clients.consumer.{ConsumerRecord, ConsumerRecords, KafkaConsumer}
import org.apache.kafka.common.TopicPartition
import org.scalatest.FunSuite
import org.scalatest.mockito.MockitoSugar
import org.mockito.Mockito._
import org.mockito.invocation.InvocationOnMock
import org.mockito.stubbing.Answer
import twitter4j.Status


class KafkaEventSourceTest extends FunSuite with MockitoSugar {


  test("subscribe should be invoked once for correct topic") {
    val topicName = "fake"
    val mockConsumer = mock[KafkaConsumer[SentimentLabel, Status]]
    val mockTime = new MockTime

    val kafkaEventSource = new KafkaEventSource(mockConsumer, topicName, mockTime)
    verify(mockConsumer, times(1)).subscribe(Collections.singletonList(topicName))
  }

  
  test("poll should return on max records") {

    val topicName = "fake"
    val mockConsumer = mock[KafkaConsumer[SentimentLabel, Status]]
    val mockTime = new MockTime

    when(mockConsumer.poll(1000)).thenAnswer(new Answer[ConsumerRecords[SentimentLabel, Status]]() {
      override def answer(invocation: InvocationOnMock): ConsumerRecords[SentimentLabel, Status] = {
        mockTime.sleep(1)
        val tp = new TopicPartition(topicName, 1)
        val record = new ConsumerRecord[SentimentLabel, Status](topicName, 0, 0, mock[SentimentLabel], mock[Status])
        val recordsMap = new util.HashMap[TopicPartition, util.List[ConsumerRecord[SentimentLabel, Status]]]()
        val recordsList = new util.ArrayList[ConsumerRecord[SentimentLabel, Status]]()
        recordsList.add(record)
        recordsMap.put(tp, recordsList)
        new ConsumerRecords[SentimentLabel, Status](recordsMap)

      }
    })

    val kafkaEventSource = new KafkaEventSource(mockConsumer, topicName, mockTime)

    val records = kafkaEventSource.poll(1000, 1)

    assert(1 === records.size)
    assert(1 === mockTime.currentMillis)
  }
} 
开发者ID:GRpro,项目名称:TwitterAnalytics,代码行数:58,代码来源:KafkaEventSourceTest.scala

示例6: mockito

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

import org.mockito.Mockito.{mock => mockitoMock}
import reflect.Manifest
import org.mockito.stubbing.Answer
import org.mockito.MockSettings

trait MockitoSugar {

  def mockito[T <: AnyRef](implicit manifest: Manifest[T]): T = {
    mockitoMock(manifest.erasure.asInstanceOf[Class[T]])
  }

  def mockito[T <: AnyRef](defaultAnswer: Answer[_])(implicit manifest: Manifest[T]): T = {
    mockitoMock(manifest.erasure.asInstanceOf[Class[T]], defaultAnswer)
  }

  def mockito[T <: AnyRef](mockSettings: MockSettings)(implicit manifest: Manifest[T]): T = {
    mockitoMock(manifest.erasure.asInstanceOf[Class[T]], mockSettings)
  }

  def mockito[T <: AnyRef](name: String)(implicit manifest: Manifest[T]): T = {
    mockitoMock(manifest.erasure.asInstanceOf[Class[T]], name)
  }
} 
开发者ID:DmytroOrlov,项目名称:library-management-system,代码行数:26,代码来源:MockitoSugar.scala

示例7: eq

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

import org.mockito.invocation.InvocationOnMock
import org.mockito.stubbing.{ Answer, OngoingStubbing }
import org.mockito.verification.VerificationMode
import org.mockito.{ Mockito => M }
import org.scalatest.mock.MockitoSugar


trait Mockito extends MockitoSugar {

  def eq[T](t: T) = org.mockito.Matchers.eq(t)
  def any[T] = org.mockito.Matchers.any[T]
  def anyBoolean = org.mockito.Matchers.anyBoolean
  def anyString = org.mockito.Matchers.anyString
  def same[T](value: T) = org.mockito.Matchers.same(value)
  def verify[T](t: T, mode: VerificationMode = times(1)) = M.verify(t, mode)
  def times(num: Int) = M.times(num)
  def timeout(millis: Int) = M.timeout(millis.toLong)
  def atLeastOnce = M.atLeastOnce()
  def atLeast(num: Int) = M.atLeast(num)
  def atMost(num: Int) = M.atMost(num)
  def never = M.never()

  def inOrder(mocks: AnyRef*) = M.inOrder(mocks: _*)

  def noMoreInteractions(mocks: AnyRef*): Unit = {
    M.verifyNoMoreInteractions(mocks: _*)
  }

  def reset(mocks: AnyRef*): Unit = {
    M.reset(mocks: _*)
  }

  class MockAnswer[T](function: Array[AnyRef] => T) extends Answer[T] {
    def answer(invocation: InvocationOnMock): T = {
      function(invocation.getArguments)
    }
  }

  implicit class Stubbed[T](c: => T) {
    def returns(t: T, t2: T*): OngoingStubbing[T] = {
      if (t2.isEmpty) M.when(c).thenReturn(t)
      else t2.foldLeft (M.when(c).thenReturn(t)) { (res, cur) => res.thenReturn(cur) }
    }
    def answers(function: Array[AnyRef] => T) = M.when(c).thenAnswer(new MockAnswer(function))
    def throws[E <: Throwable](e: E*): OngoingStubbing[T] = {
      if (e.isEmpty) throw new java.lang.IllegalArgumentException("The parameter passed to throws must not be empty")
      e.drop(1).foldLeft(M.when(c).thenThrow(e.head)) { (res, cur) => res.thenThrow(cur) }
    }
  }
} 
开发者ID:dcos,项目名称:metronome,代码行数:53,代码来源:Mockito.scala


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