对于Scala项目的单元测试,我正在编写自己的简单javax.sql.DataSource
类,它基本上只包含一个java.sql.DriverManager
实例。
我只是扩展javax.sql.DataSource
,并且在大多数情况下让Eclipse自动生成所需方法/函数的存根。
class H2DataSource extends javax.sql.DataSource {
import java.io.PrintWriter
import java.sql.DriverManager
var printWriter : PrintWriter
Class.forName("org.h2.Driver")
@throws(classOf[SQLException])
override def getLogWriter() : PrintWriter = {
printWriter
}
@throws(classOf[SQLException])
override def getLoginTimeout() : Int = {
// TODO Auto-generated method stub
0
}
@throws(classOf[SQLException])
override def setLogWriter(printWriter: PrintWriter) = {
this.printWriter = printWriter
}
@throws(classOf[SQLException])
override def setLoginTimeout(seconds: Int) = {
// TODO Auto-generated method stub
}
@throws(classOf[SQLException])
override def isWrapperFor(iface: Class[_]) : Boolean = {
// TODO Auto-generated method stub
false
}
@throws(classOf[SQLException])
override def unwrap[T](iface: java.lang.Class[_]) : T = {
// TODO Auto-generated method stub
null.asInstanceOf[T]
}
@throws(classOf[SQLException])
override def getConnection() : Connection = {
DriverManager.getConnection("jdbc:h2:myH2")
}
@throws(classOf[SQLException])
override def getConnection(user: String, password: String) : Connection = {
DriverManager.getConnection("jdbc:h2:myH2", user, password)
}
}
但是,我遇到了unwrap
函数的编译问题......编译器告诉我它没有覆盖任何东西。
以下是unwrap
的并排比较...第一次在Java中自动生成,第二次由我自己翻译成Scala。任何人都可以发现我做错了什么,这样编译器就不会认出它们是等价的吗?
@Overrride
public <T> T unwrap(Class<T> iface) throws SQLException {
return null;
}
...
@throws(classOf[SQLException])
override def unwrap[T](iface: java.lang.Class[_]) : T = {
null.asInstanceOf[T]
}
答案 0 :(得分:3)
尝试:
@throws(classOf[SQLException])
override def unwrap[T](iface: Class[T]) : T = {
null.asInstanceOf[T]
}