无法解析重载方法 thenReturn

Cannot resolve overloaded method thenReturn

我是 Scala 新手。我正在处理以下代码,其中一个 API 端点在文件中缓存值 (SomeJsonData.toString()),另一个端点从该文件中检索 (Json.parse())。使用 when-thenReturn 编写测试时,出现重载方法错误。

我哪里错了?

缓存文件内容:

{"time":92345845,"value":[{"name":"Jack","hobby":"paint"}]

CacheController.scala

def retrieveCache = {
    File(filePath).createFile()
    val source = Source.fromFile(filePath)
    val content = try source.mkString
    .....       
}

CacheControllerTest.scala

it("test") {
    val mockSuggestions = "[{\"name\":\"Jack\",\"hobby\":\"paint\"}]"
    val jsonData =Json.obj("time" -> DateTime.now(), "value" -> mockSuggestions)

    when(Source.fromFile(anyString())).thenReturn(jsonData.toString())
    // error: cannot resolve overloaded method thenReturn
}

Source.fromFile returns a BufferedSource,因此您必须将其传递给 thenReturn 中的一个,而不是字符串。