Search code examples
scalafiletestingmockitoscalatest

Cannot resolve overloaded method thenReturn


I'm new to Scala. I'm working on the following code where one API endpoint is caching value in the file(SomeJsonData.toString()) and the other endpoint is retrieving from that file(Json.parse()). When writing a test using when-thenReturn, getting an overloaded method error.

Where am I going wrong?

Cache File contents:

{"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
}

Solution

  • Source.fromFile returns a BufferedSource, so you'd have to pass it one of those in thenReturn instead of a String.