我在StackOverflow中的第一个问题 . 试图寻找答案,但我找不到与我的问题有关的任何问题 .

import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatra.test.scalatest.ScalatraFunSuite
import org.mockito.Mockito.when
import org.mockito.Matchers.any

@RunWith(classOf[JUnitRunner])
@Category(Array(classOf[DatabaseTest]))
class Test extends ScalatraFunSuite {

test("Test") {
  when(pre.findProduct(any[Seq[Sone]]))
    .thenReturn(Util.createProduct())
  when(ft.findProduct(any()))
    .thenReturn(Util.createFT())
  when(interval.findSones(any()))
    .thenReturn(Util.createInterval())

//The rest is code to utilise the above
}

我上面的测试代码用Scala编写并使用Mockito . 这是我运行时遇到的错误:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException:在这里检测到错位的参数匹配器: - > at org.Test . $ anonfun $ new $ 1(Test.scala:23)你不能在验证或存根之外使用参数匹配器 . 正确使用参数匹配器的示例:when(mock.get(anyInt())) . thenReturn(null); doThrow(new RuntimeException()) . when(mock).someVoidMethod(anyObject()); verify(mock).someMethod(contains(“foo”))此外,此错误可能会显示,因为您使用参数匹配器与无法模拟的方法 . 以下方法无法进行存根/验证:final / private / equals()/ hashCode() . 不支持在非公共父类上声明的模拟方法 .

也就是说, ft.findProduct(any())interval.findSones(any()) 都会得到相同的错误 . 暂时修复已使用固定整数,即:

test("Test") {
  when(pre.findProduct(any[Seq[Sone]]))
    .thenReturn(Util.createProduct())
  when(ft.findProduct(1))      //any() => 1
    .thenReturn(Util.createFT())
  when(interval.findSones(3))  //any() => 3
    .thenReturn(Util.createInterval())

//The rest is code to utilise the above
}

注意:还尝试使用 anyInt() ,它给出了相同的错误 .

我的问题是:我的代码有什么问题吗?希望我能够澄清我的问题 .