Return to Snippet

Revision: 66215
at March 29, 2014 03:30 by flashmac


Initial Code
/** JUNIT TEST **/

inside XDAOTest.class

@Test
	public void testUpdateDcrWithForcedexception()
	{
		ICmsDcrDAO cmsDcrDAO = LSDAOFactory.getFactory().getCmsDcrDAO();
		String fakeSelect = "This is a fake Select to provoque SQLException ?";
		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 preparedStatement = conn.prepareStatement(UPDATE_DCR);
			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{
                System.out.println("Unable to update dcr in seo_audit_data.cms_dcr table.");
            }
        }catch (SQLException e) {
        	System.out.println("Database error trying to update the DCR in table seo_audit_data.cms_dcr." + e);
        }
        return returnval;
	}

Initial URL


Initial Description
example where you need to test for an exception

Initial Title
JUNIT - testing for an exception

Initial Tags


Initial Language
Java