Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argument Exceptions should be Unit Tested?

I know this question is pretty similar to others that have been posted before but I would like to discuss this topic in a proper way.

Do you think that the "obvious" exception should be unit tested?

With obvious exception I mean for example exceptions due to null arguments or empty strings or negative numbers in situations where we the business logic of our unit make us obvious that these exceptions will always be thrown at the beginning of our method(s) before any other operation.

In other words I'm speaking of the exceptions that should be thrown after the violation of the simplest part of a class contract.

Thank you for your opinion.

like image 486
TheSENDER Avatar asked Aug 23 '10 14:08

TheSENDER


2 Answers

Absolutely. You call them "obvious," but there's nothing obvious about remembering to verify pre-conditions. In fact, most of the code I've seen in my career does not take this obvious step to prevent mayhem later on.

While you see this a lot in library code that's written for public consumption, reuse, etc., remembering to put such checks into one's own code often seems to slip by most developers. In a test-driven environment, putting tests for such conditions forces developers to properly validate input parameters on their public methods.

And let's be fair... any chance I get to write another test and see the green bar, I'm happy. :)

like image 195
RonU Avatar answered Sep 29 '22 03:09

RonU


I'd always also write a test for such "simple, obvious" things mainly because

  • An according test for these "obvious" situations is usually written very quickly and therefore I'm nearly faster in quickly writing it rather than thinking about the fact on whether to put a test or not
  • A simple test case is better than no test case
  • Test for future changes. Having a test makes sure any other dev in my team won't break my code during refactoring/bugfixing etc...
like image 22
Juri Avatar answered Sep 29 '22 02:09

Juri