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.
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. :)
I'd always also write a test for such "simple, obvious" things mainly because
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With