0

この JUnit テストに一生合格することはできません。誰かがこれが間違っている場所を指摘できますか. データの移行 (MSSQL SERVER 2005) を行っていますが、sourceDBUrl と targetDCUrl の URL を同じにして、構文エラーに絞り込みます。それが私が持っているものです、構文エラーです。クエリのテーブルの結果を比較しています

SELECT programmeapproval, resourceapproval FROM tr_timesheet WHERE timesheetid = ?

テストは常に失敗しますが、私が開発した他のjunitテストには合格します。3 つの異なる resultSetsEqual メソッドを作成しましたが、どれも機能しません。それでも、私が開発した他のいくつかの JUnit テストは合格しました。クエリ:

SELECT timesheetid, programmeapproval, resourceapproval FROM tr_timesheet

3 つの列を返します

  • timesheetid (PK,int, not null) (2240 ~ 2282 の範囲の数値が入力されます)
  • programmeapproval (smallint,not null) (すべてのフィールドに数値 1 が入力されます)
  • resourceapproval (smallint、null 以外) (すべてのフィールドに数値 1 が入力されます)

コードに埋め込まれたクエリを実行すると、programmeapproval 列と resourceapproval 列を含む 1 行のみが返され、両方のフィールドに数値 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);

}

}

/ **終了*** /

4

1 に答える 1

3

falseオブジェクトがequal互いにあったときに戻ってきました。このメソッドでは、条件を に変更しましたnot equals

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))) //added !
             {
                  return false;
             }
        }
    }
    return true;
}
于 2012-12-04T22:57:20.460 に答える