我不能让这个 JUnit 测试通过我的生活。有人可以指出哪里出了问题。我正在进行数据迁移(MSSQL SERVER 2005),但我的 sourceDBUrl 和 targetDCUrl 具有相同的 URL,以便将其缩小到语法错误。这就是我所拥有的,一个语法错误。我正在比较查询表的结果
SELECT programmeapproval, resourceapproval FROM tr_timesheet WHERE timesheetid = ?
并且测试总是失败,但通过了我开发的其他junit测试。我创建了 3 个不同的 resultSetsEqual 方法,但没有一个工作。然而,我开发的其他一些 JUnit 测试已经通过。查询:
从 tr_timesheet 中选择 timesheetid、programapproval、resourceapproval
返回三列
- timesheetid (PK,int, not null) (填充数字 2240 - 2282)
- programapproval (smallint,not null) (在每个字段中填充数字 1)
- resourceapproval (smallint,not null)(在每个字段中填充数字 1)
当我运行嵌入在代码中的查询时,它只返回一行,其中包含 programapproval 和 resourceapproval 列,并且两个字段都填充了数字 1。
我已正确安装并测试了所有 jdbc 驱动程序的连接性。根据 IDE,此时 JUnit 测试失败。
assertTrue(helper.resultSetsEqual2(sourceVal,targetVal));
这是代码:
/ *这是一个JUNIT类****?
package a7.unittests.dao;
import static org.junit.Assert.assertTrue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Types;
import org.junit.Test;
import artemispm.tritonalerts.TimesheetAlert;
public class UnitTestTimesheetAlert {
@Test
public void testQUERY_CHECKALERT() throws Exception{
UnitTestHelper helper = new UnitTestHelper();
Connection con = helper.getConnection(helper.sourceDBUrl);
Connection conTarget = helper.getConnection(helper.targetDBUrl);
PreparedStatement stmt = con.prepareStatement("select programmeapproval, resourceapproval from tr_timesheet where timesheetid = ?");
stmt.setInt(1, 2240);
ResultSet sourceVal = stmt.executeQuery();
stmt = conTarget.prepareStatement("select programmeapproval, resourceapproval from tr_timesheet where timesheetid = ?");
stmt.setInt(1,2240);
ResultSet targetVal = stmt.executeQuery();
assertTrue(helper.resultSetsEqual2(sourceVal,targetVal));
}}
/ *结束** / _
/ *这是一个普通班** /
package a7.unittests.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class UnitTestHelper {
static String sourceDBUrl = "jdbc:sqlserver://127.0.0.1:1433;databaseName=a7itm;user=a7user;password=a7user";
static String targetDBUrl = "jdbc:sqlserver://127.0.0.1:1433;databaseName=a7itm;user=a7user;password=a7user";
public Connection getConnection(String url)throws Exception{
return DriverManager.getConnection(url);
}
public boolean resultSetsEqual3 (ResultSet rs1, ResultSet rs2) throws SQLException {
int col = 1;
//ResultSetMetaData metadata = rs1.getMetaData();
//int count = metadata.getColumnCount();
while (rs1.next() && rs2.next()) {
final Object res1 = rs1.getObject(col);
final Object res2 = rs2.getObject(col);
// Check values
if (!res1.equals(res2)) {
throw new RuntimeException(String.format("%s and %s aren't equal at common position %d",
res1, res2, col));
}
// rs1 and rs2 must reach last row in the same iteration
if ((rs1.isLast() != rs2.isLast())) {
throw new RuntimeException("The two ResultSets contains different number of columns!");
}
}
return true;
}
public boolean resultSetsEqual (ResultSet source, ResultSet target) throws SQLException{
while(source.next())
{
target.next();
ResultSetMetaData metadata = source.getMetaData();
int count = metadata.getColumnCount();
for (int i =1; i<=count; i++)
{
if(source.getObject(i) != target.getObject(i))
{
return false;
}
}
}
return true;
}
public boolean resultSetsEqual2 (ResultSet source, ResultSet target) throws SQLException{
while(source.next())
{
target.next();
ResultSetMetaData metadata = source.getMetaData();
int count = metadata.getColumnCount();
for (int i =1; i<=count; i++)
{
if(source.getObject(i).equals(target.getObject(i)))
{
return false;
}
}
}
return true;
}
}
/结束** * /
/ *粘贴新课程 - 这是一个 JUNIT 测试课程* /
package a7.unittests.dao;
import static org.junit.Assert.*;
import java.sql.Connection;
import java.sql.DriverManager;
import org.junit.Test;
public class TestDatabaseConnection {
@Test
public void testConnection() throws Exception{
UnitTestHelper helper = new UnitTestHelper();
Connection con = helper.getConnection(helper.sourceDBUrl);
Connection conTarget = helper.getConnection(helper.targetDBUrl);
assertTrue(con != null && conTarget != null);
}
}
/ **结束*** /