/ Published in: Java
example where you need to test for an exception
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** JUNIT TEST **/ inside XDAOTest.class @Test public void testUpdateDcrWithForcedexception() { ICmsDcrDAO cmsDcrDAO = LSDAOFactory.getFactory().getCmsDcrDAO(); ReflectionTestUtils.setField(cmsDcrDAO, "UPDATE_DCR", fakeSelect); CmsDcr dcr = new CmsDcr(); boolean testUpdate = cmsDcrDAO.updateDcr(dcr); assertFalse(testUpdate); } /** DAO CLASS **/ inside XDAO.class private static String UPDATE_DCR = "UPDATE seo_audit_data.cms_dcr SET dcr_path = ?, countPages = ?, date_last_updated = ?, isDeleted = ? WHERE id = ?"; public boolean updateDcr(CmsDcr dcr) { conn = getDBConnection(); boolean returnval = false; try { preparedStatement.setString(1, dcr.getDcrPath()); preparedStatement.setInt(2, dcr.getCountPages()); preparedStatement.setDate(3, dcr.getDateLastUpdated()); preparedStatement.setInt(4, dcr.getIsDeleted()); preparedStatement.setInt(5, dcr.getId()); int rowsAffected = preparedStatement.executeUpdate(); conn.close(); if(rowsAffected > 0) { returnval = true; } else{ } } return returnval; }