我有以下测试用例:
test("test future") {
import scala.concurrent.ExecutionContext.global
import scala.concurrent.ExecutionContext.Implicits.global
case class Result(value: String)
val f = Future {
println("Start future")
Thread.sleep(1000)
println("End future")
Result("The Job is finished")
}
f.onComplete({
case Success(r) => println(r.value)
case Failure(ex) => ex.printStackTrace()
})
//Hold the test thread
Thread.sleep(3000)
}
抱怨Cannot find an implicit ExecutionContext
但如果我从代码中删除import scala.concurrent.ExecutionContext.global
,
那就行了。
如果我同时导入ExecutionContext.global
和ExecutionContext.Implicits.global
答案 0 :(得分:1)
你为什么不试试这个
import scala.concurrent.ExecutionContext.Implicits.{ global => ImplicitsGlobal }
import scala.concurrent.ExecutionContext.global