我试图向我的公司介绍jUnit测试,但是当我尝试为Connection建立测试时,它失败了。是的,我查看了一下,发现每个连接都是不同的,但是当我尝试使用2个“克隆连接”查询同一事物时,它仍然无法执行。
我尝试测试的功能:
public static Connection Create_DB_connection(String jdbc_class,String jdbc_url,String db_user,String db_pswrd,
java.util.Date wallDate,DateFormat wallDateFormat) throws Exception{
// <-- Create DB connection (JDBC)
System.out.println("Connecting to DB...");
DriverManager.setLoginTimeout(10);
Class.forName(jdbc_class);
Connection conn = DriverManager.getConnection(jdbc_url,db_user, db_pswrd);
System.out.println("Connected!");
System.out.println(wallDateFormat.format(wallDate));
System.out.println("");
// -->
return conn;
}
测试案例:
import static org.junit.Assert.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
@Test
public void testMicroSoft() throws Exception {
java.util.Date wallDate = new java.util.Date();
DateFormat wallDateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
String jdbc_url = "jdbc:jtds:sqlserver://";
String db_user = "";
String db_pswrd="";
String jdbc_class="net.sourceforge.jtds.jdbc.Driver";
String sql = "select idd_apr_workflow from d_apr_ticket where idd_apr_workflow = -1;";
DriverManager.setLoginTimeout(10);
Class.forName(jdbc_class);
Connection conn = DriverManager.getConnection(jdbc_url,db_user, db_pswrd);
assertEquals(conn.prepareStatement(sql), Library1.Create_DB_connection(jdbc_class, jdbc_url, db_user, db_pswrd, wallDate, wallDateFormat).prepareStatement(sql));
}
@Test
public void testORA() throws Exception {
java.util.Date wallDate = new java.util.Date();
DateFormat wallDateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
String jdbc_url = "jdbc:oracle:thin:";
String db_user = "";
String db_pswrd="";
String jdbc_class="oracle.jdbc.OracleDriver";
String sql = "select AMT from F_FIN_PL_A where AMT = 52907;";
DriverManager.setLoginTimeout(10);
Class.forName(jdbc_class);
Connection conn = DriverManager.getConnection(jdbc_url,db_user, db_pswrd);
assertEquals(conn.prepareStatement(sql), Library1.Create_DB_connection(jdbc_class, jdbc_url, db_user, db_pswrd, wallDate, wallDateFormat).prepareStatement(sql));
}
@Test
public void testIQ() throws Exception {
java.util.Date wallDate = new java.util.Date();
DateFormat wallDateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
String jdbc_url = "jdbc:sqlanywhere:DatabaseName=";
String db_user = "";
String db_pswrd="";
String jdbc_class="sybase.jdbc4.sqlanywhere.IDriver";
// here sometimes should need change sql-s if existing one doesn't work anymore
String sql = "select CHANNEL_ID from K_PDI_JOB_LOG where CHANNEL_ID = 'e60e3645-3909-4f0f-9498-66dfb83a8463';";
DriverManager.setLoginTimeout(10);
Class.forName(jdbc_class);
Connection conn = DriverManager.getConnection(jdbc_url,db_user, db_pswrd);
assertEquals(conn.prepareStatement(sql), Library1.Create_DB_connection(jdbc_class, jdbc_url, db_user, db_pswrd, wallDate, wallDateFormat).prepareStatement(sql));
}
如果需要,还提供Test Suite代码:
package Tests;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({
Create_DB_connetion_TEST.class })
public class TestSuite {}
和测试运行器代码:
package Tests;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestSuite.class);
for(Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println("Does code is error-free? Answer: " +
result.wasSuccessful());
}
}
我现在的输出是:
testORA(Tests.Create_DB_connetion_TEST):预期:但之前是:
testIQ(Tests.Create_DB_connetion_TEST):预期:但是是:
testMicroSoft(Tests.Create_DB_connetion_TEST):预期:net.sourceforge.jtds.jdbc.JtdsPreparedStatement,但之前是:net.sourceforge.jtds.jdbc.JtdsPreparedStatement
代码是否没有错误?答案:错误
我想要的输出: 代码是否没有错误?答:是的
答案 0 :(得分:0)
将assertEquals()
与使用从equals()
继承的Object
的默认实现的对象一起使用不是一个好主意。 equals()
的默认实现会检查引用是否指向相同的对象(内存中的位置基本上相同)。
您想测试您的代码建立连接这一事实的ID,您可以简单地检查是否抛出了异常并且测试查询返回了预定义的数据。
像Oracle的SELECT 1 FROM dual
一样,返回ResultSet
的一行一行一行。