Skip to content

Instantly share code, notes, and snippets.

@up1
Created August 18, 2014 03:30
Show Gist options
  • Save up1/343f4ce81b9aabc0c02d to your computer and use it in GitHub Desktop.
Save up1/343f4ce81b9aabc0c02d to your computer and use it in GitHub Desktop.
Demo for test exception with JUnit 4
public Person getPerson(int id) {
try {
String sql = "select * from person where id=?";
JdbcTemplate<Person> jdbcTemplate = new JdbcTemplate<Person>(this.dataSource);
return jdbcTemplate.executeQueryObject(sql, new Object[] {id}, new PersonRowMapper());
} catch (Exception e) {
throw new RetriveDataException("Cat not get all person");
}
}
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void findAndReadExistingPersonByUserID2() {
PersonDAO personDAO = new PersonDAO(getDataSource());
personDAO.getPerson(5);
thrown.expect(RetriveDataException.class);
}
public class RetriveDataException extends RuntimeException {
public RetriveDataException() {
super();
}
public RetriveDataException(String message) {
super(message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment