我想要对 JDBC 连接进行简单测试,我不使用框架,只使用 JDBC 和 JUnit。我可以用 JUnit 执行这个测试吗?我什至不知道如何测试加载驱动程序,请给我一些连接测试的例子。
连接客户端:
package newpackage.db;
import java.sql.Connection;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SqlConnection {
private Connection con;
private String name;
private String url;
private String password;
private String driver;
public SqlConnection() {
this.name = "root";
this.password = "12345";
this.url = "";
this.driver = "com.mysql.jdbc.Driver";
}
public Connection getConnection() {
try {
Class.forName(driver);
} catch (ClassNotFoundException ex) {
Logger.getLogger(SqlConnection.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
}
测试用例:
public void testDriverManager() {
SqlConnection conClient = new SqlConnection();
assertEquals(conClient.getConnection(),...);
or
assertTrue(conClient.getConnection(),...);
}