Suppose I have 3 classes, each of which has exactly one responsible (and method). Let them also pretend that these classes have interfaces to facilitate dependency injection. Class A calls the interface for class B, and class B calls the interface for class C. If class C throws a NotAPrimeNumberException (if, say, the int parameter is not a prime number), I would expect a unit test to happen, which does what C throws a NotAPrimeNumberException exception if the passed parameter is not a prime number. So far so good.
Currently, I am convinced that unit tests provide all the documentation necessary to understand the behavior of the test method. So the aforementioned unit test will be something like MakeSureNotAPrimeNumberExceptionIsThrownIfNumberisNotPrimeTest ().
Class B knows that class C can throw a NotAPrimeNumberException. If I want the NotAPrimeNumberException bubble to exit class B, should I write a unit test for class B to somehow verify that a NotAPrimeNumberException is thrown in some cases? What about Class A? If A also allows a NotAPrimeNumberException bubble, if it also has a unit test for this?
I am worried that if I DO NOT write unit test in class B, then consumers for class B will not know that class B may raise this type of exception. However, if I write unit test, itβs a little silly that I need to get a class B call to throw a NotAPrimeNumberException exception so as NOT to throw an exception in class B. If I donβt write a unit test, what is the appropriate way, if any, to document in the API, what this exception can arise?
Bonus question - how do you do this in NUnit with Rhino-mocks? This, of course, depends on the first two questions.
source share