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


Scala OngoingStubbing类代码示例

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


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

//设置package包名称以及导入依赖的类
package com.ulasakdeniz.hakker.base

import org.mockito.InOrder
import org.mockito.stubbing.{OngoingStubbing, Stubber}
import org.mockito.verification.VerificationMode
import org.scalatest.mockito.MockitoSugar

import scala.reflect.ClassTag

trait UnitSpec extends SuperSpec with MockitoSugar with MockitoMocker

// a copy of the specs2/mock/src/main/scala/org/specs2/mock/mockito/MockitoMocker.scala

trait MockitoMocker {
  def verify(mode: VerificationMode): List[Int] =
    org.mockito.Mockito.verify(org.mockito.Mockito.mock(classOf[List[Int]]), mode)

  def spy[T](m: T): T                   = org.mockito.Mockito.spy(m)
  def when[V](v: V): OngoingStubbing[V] = org.mockito.Mockito.when(v)
  def times(i: Int): org.mockito.internal.verification.Times =
    org.mockito.Mockito.times(i).asInstanceOf[org.mockito.internal.verification.Times]
  def any[T](implicit m: ClassTag[T]): T = org.mockito.Matchers.any(m.runtimeClass).asInstanceOf[T]

  def verify[M <: AnyRef](inOrder: Option[InOrder], m: M, v: VerificationMode): M = {
    inOrder match {
      case Some(ordered) => ordered.verify(m, v)
      case None          => org.mockito.Mockito.verify(m, v)
    }
  }

  def verify[M](m: M, v: VerificationMode): M = org.mockito.Mockito.verify(m, v)
  def doReturn[T](t: T): Stubber              = org.mockito.Mockito.doReturn(t)
  def doThrow[E <: Throwable](e: E): Stubber  = org.mockito.Mockito.doThrow(e)
} 
开发者ID:ulasakdeniz,项目名称:hakker,代码行数:35,代码来源:UnitSpec.scala

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