我在单元测试expected:<interface java.sql.Connection> but was:<class com.mysql.jdbc.JDBC4Connection>
来自我的代码:
@Test
public void connectionTest() throws SQLException{
Connection conn = ConnectionManager.createConnection();
assertEquals(Connection.class, conn.getClass());
conn.close();
}
我获取连接的模拟类:
public class ConnectionManager {
@SuppressWarnings("unused")
public static Connection createConnection() throws SQLException {
String url = "jdbc:mysql://localhost:3306/library";
String name = "root";
String password = "root";
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(url, name, password);
} catch (ClassNotFoundException e) {
if (connection != null){
connection.close();
}
throw new SQLException(e);
}
return connection;
}
}
我的代码出了什么问题?
答案 0 :(得分:4)
你的断言从根本上说是错误的。
Connection.class
是一个界面
conn.getClass()
将返回一个实现该接口的具体类。