[英]What kind of JUnit test can you do with this bit of code?
public static void sql() {
String url = "jdbc:msql://carthage.imaginary.com/ora";
Connection con = null;
try {
String driver = "com.imaginary.sql.msql.MsqlDriver";
Class.forName(driver).newInstance();
} catch (Exception e) {
System.out.println("Failed to load mSQL driver.");
return;
}
try {
con = DriverManager.getConnection(url, "borg", "");
Statement select = con.createStatement();
ResultSet result = select
.executeQuery("SELECT test_id, test_val FROM test");
System.out.println("Got results:");
while (result.next()) { // process results one row at a time
int key = result.getInt(1);
String val = result.getString(2);
System.out.println("key = " + key);
System.out.println("val = " + val);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
I have to do some tests with Junit for a bunch of SQL methods and I have no idea what I could do with this bit of code. 我必须对Junit进行一堆SQL方法的测试,但我不知道如何使用这段代码。 I know how to use assert and expected exception, but those are useless here. 我知道如何使用断言和预期的异常,但是这里没有用。
Firstly, I suggest you write some test cases. 首先,我建议您编写一些测试用例。 And then you implemente the methods that are cases needed(refactor this bit of code into methods). 然后,实现需要案例的方法(将这段代码重构为方法)。
firstly i would re-factor the code into two part. 首先,我将代码分解为两部分。
1 .create connection
2. Get connection object and do operation.
you are combining both in your code. 您将两者都结合在您的代码中。
i would do test cases as below 我会做如下测试用例
firstly write a junit class for testing connection. 首先编写一个junit类来测试连接。
Class GenericDAO
public static Connection getConnection(){
do your sql steps and return connection object
}
and your test class should be 而你的测试课应该是
public class TestConnection {
public void testConnection(){
Connection con = GenericDAO.getConnection();
assert here for not null
Assert.assertForNotNull(con );
}
add test cases for other boundary condition
}
later you can write individual test cases other table sql query 稍后您可以编写其他表sql查询的个别测试用例
for test table 用于测试表
public class TestDAO
//either you pass conection object or inherit from parent class
public String retrieveResults(){
do opertaion here
}
}
and you will have separate junit for this 您将为此拥有一个单独的junit
class TestDAOTest{
public void testForResults(){
String str = testDAO.retreiveResults();
do assertrtion as per your wise
assert for null, or specific string, negative test cases etc. Note , each can be a separate test cases, by this we are covering boundary conditions
}
}
lastly, you can combine the tests into test-suite, and run together 最后,您可以将测试合并到测试套件中,然后一起运行
http://www.tutorialspoint.com/junit/junit_suite_test.htm http://www.tutorialspoint.com/junit/junit_suite_test.htm
Your method does not return anything, so I think this your method is just to play around with? 您的方法不返回任何内容,所以我认为您的方法只是在玩弄? Normally methods in your DAO will return , insert, update or delete values. 通常,DAO中的方法将返回,插入,更新或删除值。 These are things you can test. 这些是您可以测试的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.