The page assembles tips on converting old tests to JUnit. (Navigate back to the DerbyTesting page).

Table of Contents

How do I do X?

Check the "Conversion of writing JUnit tests FAQ" on the KillDerbyTestHarness page for a variety of Question and Answer topics.

My new junit test compiles and runs just fine -- why doesn't it actually execute?

Many of the old tests take a connection argument, such as:

static void testLongBinary(Connection conn)

When you copy the code into a JUnit test, remember to remove the connection argument:

static void testLongBinary ()

Are there examples?

The IntroToJUnit wiki page has some starter examples. Also, DerbyTesting lists pages that track conversions by area (client, lang, jdbcadpi, etc.). Check the completed code for those other conversions.

How do you delete the old harness files after a test is converted to junit?

Below is a checklist that was posted to derby-dev:

How do I get the expected SQLStates?

or

Common mistakes

try {
  s.execute(command);
}
catch (SQLException e)
{
   assertSQLState("42502", e);
}

It should be:

try {
  s.execute(command);
  fail("Command expected to fail" + command);
}
catch (SQLException e)
{
   assertSQLState("42502", e);
}

or for this specific example a utility method already exists

   assertStatementError("42502",s,command);

try {
   PreparedStatement ps = prepareCall(sql);
   ps.setInt(1, 23);
   ps.setString(2, 'hello');
   // this call should fail
   ps.executeUpdate();
   fail("expected the executeUpdate() to fail");
} catch (SQLException e) {
   assertSQLState("07000", e);
}

Instead move the methods expected to work outside of the try catch block.

PreparedStatement ps = prepareStatement(sql);
ps.setInt(1, 23);
ps.setString(2, 'hello');
try {
   ps.executeUpdate();
   fail("expected the executeUpdate() to fail");
} catch (SQLException e) {
   assertSQLState("07000", e);
}

  } finally {
    if (rs != null)
    { try {rs.close()} catch (SQLException e) {} }
  }

Just call the close methods in line with the rest of the code, no need for any finally blocks.

   PreparedStatement ps = prepareStatement(sql);
   ...
   ResultSet rs = ps.executeQuery();
   ...
   rs.close();
   ps.close();

When writing tests is there a short cut to creating the ResultSet two dimensional arrays?

You can use org.apache.derbyTesting.junit.Utilities.showResultSet(ResultSet rs) to print out the two dimensional array and then cut and paste into your code.

Where is the javadoc for the derby junit infrastructure

The javadoc for the tests and test infrastructure is generated daily and posted here

ConvertOldTestToJunitTips (last edited 2009-09-20 22:11:44 by localhost)