使用 java 执行查询时,返回多行时如何验证结果文本?
When executing a query using java, how can I verify the results text when multiple rows are returned?
我正在使用 JDBC 连接到 SQL 并执行查询。我是 运行 的查询将 return 4 个结果,我可以使用以下代码打印它们。
Statement stmt = (Statement) con.createStatement();
ResultSet result = (ResultSet) stmt.executeQuery("SELECT TransText FROM Glossary WHERE GlossID IN (SELECT GlossID FROM DBO.Resource WHERE TypeID=6)");
while(result.next()){
String X = result.getString("TransText");
System.out.println(X);
}
这是打印的结果
TXT File
Multiple Languages
Resource A
Resource B
如何验证从查询中 return 编辑的所有 4 个结果的文本?我尝试使用 assertEquals,但它仅验证循环中 returned 的第一行文本(TXT 文件)
Statement stmt = (Statement) con.createStatement();
ResultSet result = (ResultSet) stmt.executeQuery("SELECT TransText FROM Glossary WHERE GlossID IN (SELECT GlossID FROM DBO.Resource WHERE TypeID=6)");
while(result.next()){
String X = result.getString("TransText");
System.out.println(X);
Assert.assertEquals(X, "TXT File\nMultiple Languages\nResource A\nResource B");
}
将每个 String
添加到 Set<String>
。然后,创建一个替代 Set<String> expected
并用预期值填充它。然后,比较两个 Set<String>
是否相等。我建议 Set
而不是 List
,因为您的查询结果可能以不同的顺序出现。
测试应该是这样的:
@Test
public void test() {
List<String> results = retrieveFromDB();
//name doesn't refer to ResultSet interface, it's just a coincidence
Set<String> resultSet = new HashSet<String>(results);
Set<String> expectedResults = new HashSet<String>(Arrays.asList("expected1", "expected2", ...));
Assert.assertEquals(expectedResults, resultSet);
}
此方法假设查询中没有重复值,或者查询中重复值的数量无关紧要。
List<String> actualList = new ArrayList<>();
while(result.next()){
actualList.add(result.getString("TransText"));
}
List<String> expectedList = Arrays.asList("TXT File",
"Multiple Languages",
"Resource A",
"Resource B");
assertEquals(expectedList, actualList);
如果顺序无关紧要,请使用集合而不是列表。
根据结果创建列表,然后比较该列表
List<String> list = new ArrayList<String>();
while(result.next()){
list.add(result.getString("TransText"));
}
然后遍历列表并比较它是否与您的结果匹配。
创建您的预期结果列表。
List<String> list1 = new ArrayList<String>();
// 按顺序添加您的加急内容
然后使用 asset 或 equals 方法比较列表。
if(list.equals(list1)){
// write your logic here
}
或
Assert.assertEquals(list1, list);
我会将所有结果读入列表,然后使用 AssertJ 等库编写断言(该库可在 http://joel-costigliola.github.io/assertj/ 找到)。
举个例子:
List<String> queryResults = new ArrayList<>();
Statement stmt = (Statement) con.createStatement();
ResultSet result = (ResultSet) stmt.executeQuery("SELECT TransText FROM Glossary WHERE GlossID IN (SELECT GlossID FROM DBO.Resource WHERE TypeID=6)");
while(result.next()){
String X = result.getString("TransText");
queryResults.add(X);
}
// AssertJ assertion
assertThat(queryResults).hasSize(4).contains('TXT file', 'Multiple Languages', 'Resource A', 'Resource B');
我正在使用 JDBC 连接到 SQL 并执行查询。我是 运行 的查询将 return 4 个结果,我可以使用以下代码打印它们。
Statement stmt = (Statement) con.createStatement();
ResultSet result = (ResultSet) stmt.executeQuery("SELECT TransText FROM Glossary WHERE GlossID IN (SELECT GlossID FROM DBO.Resource WHERE TypeID=6)");
while(result.next()){
String X = result.getString("TransText");
System.out.println(X);
}
这是打印的结果
TXT File
Multiple Languages
Resource A
Resource B
如何验证从查询中 return 编辑的所有 4 个结果的文本?我尝试使用 assertEquals,但它仅验证循环中 returned 的第一行文本(TXT 文件)
Statement stmt = (Statement) con.createStatement();
ResultSet result = (ResultSet) stmt.executeQuery("SELECT TransText FROM Glossary WHERE GlossID IN (SELECT GlossID FROM DBO.Resource WHERE TypeID=6)");
while(result.next()){
String X = result.getString("TransText");
System.out.println(X);
Assert.assertEquals(X, "TXT File\nMultiple Languages\nResource A\nResource B");
}
将每个 String
添加到 Set<String>
。然后,创建一个替代 Set<String> expected
并用预期值填充它。然后,比较两个 Set<String>
是否相等。我建议 Set
而不是 List
,因为您的查询结果可能以不同的顺序出现。
测试应该是这样的:
@Test
public void test() {
List<String> results = retrieveFromDB();
//name doesn't refer to ResultSet interface, it's just a coincidence
Set<String> resultSet = new HashSet<String>(results);
Set<String> expectedResults = new HashSet<String>(Arrays.asList("expected1", "expected2", ...));
Assert.assertEquals(expectedResults, resultSet);
}
此方法假设查询中没有重复值,或者查询中重复值的数量无关紧要。
List<String> actualList = new ArrayList<>();
while(result.next()){
actualList.add(result.getString("TransText"));
}
List<String> expectedList = Arrays.asList("TXT File",
"Multiple Languages",
"Resource A",
"Resource B");
assertEquals(expectedList, actualList);
如果顺序无关紧要,请使用集合而不是列表。
根据结果创建列表,然后比较该列表
List<String> list = new ArrayList<String>();
while(result.next()){
list.add(result.getString("TransText"));
}
然后遍历列表并比较它是否与您的结果匹配。
创建您的预期结果列表。
List<String> list1 = new ArrayList<String>();
// 按顺序添加您的加急内容
然后使用 asset 或 equals 方法比较列表。
if(list.equals(list1)){
// write your logic here
}
或
Assert.assertEquals(list1, list);
我会将所有结果读入列表,然后使用 AssertJ 等库编写断言(该库可在 http://joel-costigliola.github.io/assertj/ 找到)。
举个例子:
List<String> queryResults = new ArrayList<>();
Statement stmt = (Statement) con.createStatement();
ResultSet result = (ResultSet) stmt.executeQuery("SELECT TransText FROM Glossary WHERE GlossID IN (SELECT GlossID FROM DBO.Resource WHERE TypeID=6)");
while(result.next()){
String X = result.getString("TransText");
queryResults.add(X);
}
// AssertJ assertion
assertThat(queryResults).hasSize(4).contains('TXT file', 'Multiple Languages', 'Resource A', 'Resource B');