Do you think that this is a good way for testing exceptions? Any suggestions?
Exception exception = null;
try{
//I m sure that an exeption will happen here
}
catch (Exception ex){
exception = ex;
}
Assert.IsNotNull(exception);
I'm using MS Test.
Best Answer
I have a couple of different patterns that I use. I use the
ExpectedException
attribute most of the time when an exception is expected. This suffices for most cases, however, there are some cases when this is not sufficient. The exception may not be catchable - since it's thrown by a method that is invoked by reflection - or perhaps I just want to check that other conditions hold, say a transaction is rolled back or some value has still been set. In these cases I wrap it in atry/catch
block that expects the exact exception, does anAssert.Fail
if the code succeeds and also catches generic exceptions to make sure that a different exception is not thrown.First case:
Second case: