Testing for an error using exception handling - the stupid way

/ Published in: Java
Save to your folder(s)

I was refactoring code lately, changed a method to return null instead of throwing an error - yet this test kept working. I did not get why for quite some time.

Lesson learned: Never rely on Eclipse generating a try-catch-block without actually looking what kind of exception or error it catches.

Copy this code and paste it in your HTML
  1. public void testShouldNotFindAfterDeletion(){
  2. manager.createContact(contact);
  3. manager.deleteContact(contact.getID());
  5. /* now try to read it again to make sure the delete request works: */
  7. try {
  8. manager.getContact(contact.getID()); //this throws an AssertionError.
  9. fail("Should not reach this step, should throw exception first"); //mistake that should have been obvious: This also does.
  10. } catch (AssertionError e){
  11. //succeed
  12. };
  13. }

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.