Scala SBT在完成的测试上断开数据库客户端

Scala SBT在完成的测试上断开数据库客户端,scala,sbt,Scala,Sbt,我正在尝试执行sbt任务主项目中的代码: lazy val disconnectClients = taskKey[Unit]("Disconnect postgres clients after tests.") disconnectClients := { (testLoader in Test).value .loadClass("com.companyname.DBDisconnector") .getMethod("close") .invoke(null

我正在尝试执行sbt任务主项目中的代码:

lazy val disconnectClients = taskKey[Unit]("Disconnect postgres clients after tests.")

disconnectClients := {
  (testLoader in Test).value
    .loadClass("com.companyname.DBDisconnector")
    .getMethod("close")
    .invoke(null).asInstanceOf[Unit]
}
其中,隔离开关:

class DBDisconnector {
  ...
  def close(): Unit = {
    println("-------> DISCONNECT <--------")
    ds.close()
  }
}
DBD级隔离开关{
...
def close():单位={

println(“---->DISCONNECT将实例化对象传递给
invoke
,而不是像这样传递
null

lazy val disconnectClients = taskKey[Unit]("Disconnect postgres clients after tests.")
disconnectClients := {
  val loader = (testLoader in Test).value
  val cls = loader.loadClass("example.DBDisconnector")
  val disconnector = cls.newInstance()
  cls.getMethod("close").invoke(disconnector)
}
是实现类似清理的另一种方法:

testOptions in Test += Tests.Cleanup { loader =>
  val cls = loader.loadClass("example.DBDisconnector")
  val disconnector = cls.newInstance()
  cls.getMethod("close").invoke(disconnector)
}