The journal of Paul M. Watson.

Tuesday, April 11, 2006

Testing both ways

I have just been taught the lesson that when testing your code, test for failures too.

Take a method that removes a record from the database. You pass in the Id of the record and it DELETEs it. Initially I tested for an invalid Id (e.g. -1) and for a successful delete. I never tested to see what happens when you pass in a valid Id but an Id that doesn't occur in the database. In this case the code ran as if it were successful. Really it should throw an exception as that Id does not exist in the database.

Test both ways!

2 Comments:

Anonymous Steve Crane said...

You make a good point but your example raises an interesting question.

If you request deletion of ID n then the successful result is that the row with ID n no longer exists after the operation. This is the case if ID n did not exist in the first place so can it really be said to be an error if a non-existent item is deleted?

10:26 AM

 
Blogger Paul Watson said...

Good point Steve.

I think though throwing an exception when no rows were affected gives better information to the caller than just silently continuining as if a row was affected. The caller can then choose what to do with that info.

1:20 PM

 

Post a Comment

Links to this post:

Create a Link

<< Home