I. G. wrote about an incident that caused him to nearly give himself a concussion from a *headdesk* moment. A newly developed system was meticulously designed, coded and tested to obscene levels; all appeared well.

Unfortunately, upon deployment, it began acting erratically, returning incorrect results from numerous database queries. After many debugging sessions and code walkthroughs, it was discovered that the developers had used the following pattern for all the database DAO tests:

  public interface SomeDao {
    public List<String> getData( /* args here */ );
  }

  public class SomeDaoImpl implements SomeDao {
    @Override 
    public List<String> getData( /* args here */ ) {
      String            sql     = "select ... from ... where ...";
      List<String>      results = new ArrayList<String>();
      Connection        con     = ...;
      PreparedStatement ps      = con.prepareStatement(sql);
      // set args in query here
      ResultSet         rs      = ps.executeQuery();
      while (rs.next()) {
        results.add(rs.getString(1));
      }
      // close stuff
      return results;
    }
  }

  public class SomeDaoTest {
    private final static String RESULT_ROW_1 = "String 1 data ...";
    private final static String RESULT_ROW_2 = "String 2 data ...";
    private final static String RESULT_ROW_3 = "String 3 data ...";

    class SomeDaoImplTest implements SomeDao {
      @Override
      public List<String> getData( /* args here */ ) {
        List<String> results = new ArrayList<String>();
        results.add(RESULT_ROW_1);
        results.add(RESULT_ROW_2);
        results.add(RESULT_ROW_3);
        return results;
      }
    }

    @Test
    public void testSomeDao() {
      SomeDao dao = new SomeDaoImplTest();

      List<String> results = dao.getData( /* args here */ );

      Assert.assertNotNull(results);
      Assert.assertEquals(3,results.size());
      Assert.assertEquals(RESULT_ROW_1, results.get(0));
      Assert.assertEquals(RESULT_ROW_2, results.get(1));
      Assert.assertEquals(RESULT_ROW_3, results.get(2));
    }
  }

When queried, the developers said that the tests took too long to run when they hit the database, so they created stub-DAO's to return canned data, because they assumed the SQL queries would be correct and didn't need testing.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!