1

I'm testing two prepared statement as you will see below and I want it to show that they are the same using the assertEquals but its throwing me an error that the result and expectedResult are not equal.

public PreparedStatement setSQL(String sql) throws SQLException {

try {

pst = connection.prepareStatement(sql);

} catch (SQLException e) {

e.printStackTrace();

}

return pst;

}


public void testSetSQL() throws SQLException {

        String sql = "SELECT * FROM student INNER JOIN member ON student.ssn=member.ssn WHERE ssn = ?";

        result = dbconn.setSQL(sql);

        // expsql = ;

        expectresult = connection.prepareStatement("SELECT * FROM student INNER JOIN member ON student.ssn=member.ssn WHERE ssn = ?");

        assertSame(expectresult, result);
Richard Jimenez
  • 177
  • 1
  • 1
  • 11

1 Answers1

0

Refer this SO Question and its answers to understand what assertSame means. It checks for equality of two object references and that is certainly not the case for your code since two distinct connection.prepareStatement(...) calls would give you two different references/objects.

I am not sure how does equality of PreparedStatement works, you can check its source code for equals() method (PreparedStatement is simply an interface in JDK, its implementation is provided by driver providers ) and see of assertEquals is what you need.

Your code is working as expected.

Quoting from prepareStatement(java.lang.String),

Returns: a new default PreparedStatement object containing the pre-compiled SQL statement

Sabir Khan
  • 9,826
  • 7
  • 45
  • 98