Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8929215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:37:21+00:00 2026-06-15T08:37:21+00:00

I CANNOT get this JUnit Test to pass for the life of me. Can

  • 0

I CANNOT get this JUnit Test to pass for the life of me. Can somebody point out where this has gone wrong. I am doing a data migration(MSSQL SERVER 2005), but I have the sourceDBUrl and the targetDCUrl the same URL so to narrow it down to syntax errors. So that is what I have, a syntax error. I am comparing the results of a table for the query

SELECT programmeapproval, resourceapproval FROM tr_timesheet WHERE timesheetid = ?

and the test always fails, but passes for other junit tests I have developed. I created 3 diffemt resultSetsEqual methods and none work. Yet, some other JUnit tests I have developed have PASSED. THE QUERY:

SELECT timesheetid, programmeapproval, resourceapproval FROM tr_timesheet

Returns three columns

  • timesheetid (PK,int, not null) (populated with a range of
    numbers 2240 – 2282)
  • programmeapproval (smallint,not null) (populated with the
    number 1 in every field)
  • resourceapproval (smallint, not null) (populated with a number
    1 in every field)

When I run the query that is embedded in the code it only returns one row with the programmeapproval and resourceapproval columns and both field populated with the number 1.

I have all jdbc drivers correctly installed and tested for connectivity. The JUnit Test is failing at this point according to the IDE.

assertTrue(helper.resultSetsEqual2(sourceVal,targetVal));

This is the code:

/*THIS IS A JUNIT CLASS****?

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));
}}

/*END**/

/*THIS IS A REGULAR CLASS**/

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;
}
}

/END***/

/*PASTED NEW CLASS – THIS IS A JUNIT TEST CLASS*/

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);

}

}

/**END***/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:37:22+00:00Added an answer on June 15, 2026 at 8:37 am

    You returned false when objects were equal to each other. In this method I changed condition to 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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For the life of me I cannot get this bug figured out - I
I just cannot get this to work, would appreciate if someone can help. So
I can't understand this... Cannot get this code to run and I've no idea
I cannot get this link to be clickable for the life of me. When
For some reason i cannot get this if statement to work if (htmlCode.Contains(Sign out)
I cannot get this function to fire off... I'm thinking I need a fresh
I cannot get this to work, here is code that I found in another
Hey guys I simply cannot get this to work. I have some content that
Ok, I cannot get this. I've looked at it and I don't see why
Ok, I've tried about near everything and I cannot get this to work. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.