【问题标题】:Cannot resolve overloaded method thenReturn无法解析重载的方法 thenReturn
【发布时间】:2020-08-11 18:04:34
【问题描述】:

我是 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
}

【问题讨论】:

    标签: scala file testing mockito scalatest


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2022-08-02
      • 2021-03-20
      • 2019-09-17
      • 1970-01-01
      • 2021-03-01
      • 2021-03-06
      • 1970-01-01
      • 2017-10-09
      • 2011-10-11
      相关资源
      最近更新 更多