【发布时间】:2018-10-20 14:22:39
【问题描述】:
下面是我的 2 个 SQL Server 表。
地址
addressId | addressName |
2 | testAddress |
友情链接
linkId | clientId | addressId |
1 | 4 | 2 |
我正在尝试编写一个使用 SQL 子查询来检查 taddress 中是否存在记录的 java 测试。
例如:
SELECT COUNT(*) FROM Taddress
WHERE addressId =
(SELECT addressId FROM Tlink
WHERE clientId = param)
目前,当我运行以下测试时,即使 tLink 中不存在 clientId 参数,测试也始终通过。
try {
dbAccessSetUp();
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM taddress a INNER JOIN tlink l on a.address_id = l.internal_address_id WHERE l.ext_client_id =" + this.clientNo);
if(!rs.next()) {
fail("Record does not exist in taddress based on ExtClientNo");
}
int count = 0;
while(rs.next()) {
count = rs.getInt(1);
System.out.println("number of count : " + count);
assertTrue(0 < count);
}
rs.close();
} catch(SQLException se) {
se.printStackTrace();
assertEquals(true, false);
} catch(Exception e) {
e.printStackTrace();
assertEquals(true, false);
} finally {
try {
if(stmt!=null) stmt.close();
} catch(SQLException se2) {
assertEquals(true, false);
}
try {
if(conn!=null) conn.close();
} catch(SQLException se) {
se.printStackTrace();
assertEquals(true, false);
}
}
【问题讨论】:
-
你是否传递了一个特定的值来代替参数?
-
@Mohan 嗨,是的,这将是从黄瓜功能文件传入的值。因此,如果一个有效值(与 tLink 中的 clientId 匹配的值)作为参数传入,那么我想使用该行的 addressId 来计算 Taddress 中的行数。另外,如果一个无效值(与 tLink 中的 clientId 不匹配的值),那么我希望测试失败
-
基本上,如果 tLink 中没有该 clientId 的记录,我希望测试失败,如果 tAddress 中也没有匹配记录,我希望测试失败
-
您是否进行了任何调试以查看
rs.getInt(1)为您的测试用例返回的内容? -
我很惊讶测试总是通过。您可能需要像 MatBailie 指出的那样打印 count 的值,看看它是否返回大于零的无效值
标签: java sql sql-server subquery