Hi
I am wondering why should you unit test exceptions? Like I thought you should not be testing code you did not write.
In a sense to me you're not really writing the exception. Sure you put it in a try/catch but should you not assume that the try catch will work and you will catch that exception.
Like for instance I have some GetUser(...) from Asp.net membership. If no user is found it throws a NullReferenceException.
So I just have
try
{
MembershipUser user = Membership.GetUser(...);
}
catch(NullReferenceException ex)
{
throw new NullReferenceException()
}
Normally I would just catch the message right there and print out the ex.Message but I need to throw it for unit testing.
So why would I need to test this I assume the try/catch works. So why check if it is thrown?
I am not very good with Exceptions and Unit testing. So maybe I am missing something.
In this case the code is a method that gets called by another method. So in my other method I could surround it with another try/catch and print the ex.Message to the webpage or log file.
But if I had this code in one method and the it gets thrown where would I catch it?