我正在编写查询SQL Server数据库的黄瓜测试。
下面的查询应该失败,因为表中不存在该记录。但是,测试通过了。
try {
Class.forName(JDBC_DRIVER);
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL,USER,PASS);
System.out.println("Connected database successfully...");
stmt = conn.createStatement();
String sql = "SELECT id FROM tclientlink WHERE id = '0000060115123'";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()) {
int id = rs.getInt("goald_client_id");
System.out.print("ID IS HERE: " + id);
assertEquals(60115, id);
}
rs.close();
} catch(SQLException se) {
se.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
if(stmt!=null) stmt.close();
} catch(SQLException se2) {
}
try {
if(conn!=null) conn.close();
} catch(SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
答案 0 :(得分:1)
您在下一行有NullPointerException
:
int id = rs.getInt("goald_client_id");
并转到异常块,只需在所有异常块中添加以下语句
assertEquals(true, false);
答案 1 :(得分:0)
您的while循环未执行,因为结果集中没有记录,并且默认情况下通过了测试用例, 请进行以下更改
while(rs.next()) {
int id = rs.getInt("goald_client_id");
System.out.print("ID IS HERE: " + id);
assertEquals(60115, id);
}
使用
更改以上代码int noOfRecords = 0;
while(rs.next()) {
int id = rs.getInt("goald_client_id");
System.out.print("ID IS HERE: " + id);
assertEquals(60115, id);
noOfRecords ++;
}
if( noOfRecords == 0 ) {
assertEquals(true, false);
}