views:

61

answers:

2

I have a question that may seem stupid and simple, but I hardly have any idea how to proceed with it.

My question is:

How can I modify the exception message and customize it such that I still have my unit testing passing?

Actually I want to customize the exception message to "Student "Johny" had related files!" and as modified the API exception message, the unit testing failing.

Johny is a variable that may change...

Any help how I can achieve the above. Thanks


In my test class I am having

        [ExpectedException(ExceptionType = typeof(Exception), ExpectedMessage = "The DELETE statement conflicted with the REFERENCE constraint \"FK_Issue_Priority\"")]

Actually I am using NHibernate and in my API I am handling exception as follows:

catch (NHibernate.ADOException exception)
        {
            if (exception.InnerException.GetType().Equals(typeof(System.Data.SqlClient.SqlException)))
            {
                if (exception.InnerException.Message.Contains("FK_Issue_Priority"))
                {
                    throw new Exception("The DELETE statement conflicted with the REFERENCE constraint \"FK_Issue_Priority\"");
                }
                else
                {
                    throw new Exception("A database error occurred while trying to add the customer to project relation please the see inner exception for details", exception.InnerException);
                }
            }
            else
            {
                throw exception;
            }
        }
+2  A: 

I don't test the exact content of exception messages in my unit tests for exactly this reason -- they tend to be variable.

Instead you have two options:

  1. Derive a new Exception based class expressly for throwing in this method (for example a 'RelatedFilesExistedException' class). Unit testing can simply check that the correct exception type is being returned without having to worry about exactly matching the message text.

  2. Only partially match the exception message (for which you will have to write your own testing code and not reply on the ExpectedException attribute).

Dr Herbie
Thanks for replying. Can I have an example how to do that?
In Visual Studio, create a new class file and delete the class definition. Then use the 'Exception' snippet to create a new exception class (type 'Exception' and then TAB to fire the snippet), rename the class to 'RelatedFilesExistException'. In you code, throw new RelatedFilesExistException instead of throw new Exception.In you unit test use [ExpectedException(ExceptionType = typeof(RelatedFilesExistException )]
Dr Herbie
A: 
  1. Create different exception classes for different things like Dr Herbie suggest.
  2. I wouldn't use exceptions for normal control flow. There are other language constructs for that, like if-else. Exceptions are for exceptional behavior.
  3. I wouldn't make it possible for a user to click on a button they are not allowed to click on. Displaying a message instead of a button can be more user friendly.
Paco