我正在尝试使用when
模拟一个函数,但我一直收到此错误
2 matchers expected, 1 recorded:
-> at com.concrete.test.LuigiHistoryServiceTest.testHistory(LuigiHistoryServiceTest.scala:23)
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
这是我的测试代码
val service: LuigiHistoryService = spy(new LuigiHistoryService)
when(service.runQuery(Matchers.anyString(), Matchers.any[ResultSet => Seq[ExtendedTaskStatus]].apply))
.thenReturn(Seq.empty)
这是方法的签名
def runQuery[T](query: String, fn: ResultSet => T): Seq[T] = {/* */}
答案 0 :(得分:1)
我尝试了以下方法,看起来不错:
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.mockito.MockitoSugar
import org.scalatest.{FunSuite, Matchers}
import org.mockito.Mockito._
import org.mockito.Matchers._
@RunWith(classOf[JUnitRunner])
class MyTest extends FunSuite with Matchers with MockitoSugar {
case class ResultSet()
case class ExtendedTaskStatus()
class LuigiHistoryService {
def runQuery[T](query: String, fn: ResultSet => T): Seq[T] = List()
}
test("simple test"){
val service: LuigiHistoryService = spy(new LuigiHistoryService)
when(service.runQuery(anyString(), any[ResultSet => Seq[ExtendedTaskStatus]].apply))
.thenReturn(Seq.empty)
def fun(r: ResultSet) = List(ExtendedTaskStatus)
service.runQuery("hi", fun)
verify(service)
}
}
如果此代码不能解决您的问题,您可以更新此代码吗?