Asserting the Expected Exception Message

Posted: April 8, 2013 in .Net, Delegates, Generics, Unit Testing
Tags: , , , ,

If you do a quick google search you will realize a lot of people out there have the need to be able to unit test that a piece of code throws an exception with a specific message. Unfortunately the ExpectedExceptionAttribute attribute does not provide this feature. It does have a “message” parameter but its use is to define the message the developer will see when the test fails.

All the people with this need, myself included, have searched long and hard for a way to implement an attribute with this functionality or, at the very least, a way to execute this kind of test reliably and in a way that it can be reused consistently for each possible case.

Most implementations I have found are different flavors of catching the exception and comparing the type and message to an expected one, but they are limited in the number or type of parameters, or on the return type they support. I did once find the implementation of an attribute but was a little disappointed since I thought it was too much code for something as simple as the objective at hand. Let me be very clear about the fact that I am NOT saying this is a bad option nor am I discouraging its use in any way or form. I am only saying that, in my very personal opinion, it is too much code for something so simple and, it is code that you should understand how it works since you might have the need to maintain it.

So because of this I came up with another implementation by using delegates which is smaller than the attribute one and supports both actions and functions with as many parameters as needed. It does have the disadvantage though that its use might be a little cryptic. I always strive for code readability so decorating the test method with an attribute may be a little better. Here is the code, some examples on its use and, for completeness, a Foo class to test it:

This is the code that we are interested in:


public static class CustomAssert
{
   public static void IfExceptionIs(Exception expected, Delegate action, params object[] parameters)
   {
      try
      {
         action.DynamicInvoke(parameters);
         Assert.Fail(string.Format("Expected exception of type <{0}>; with message <{1}> but none was thrown.", expected.GetType().FullName, expected.Message));
      }
      catch (Exception ex)
      {
         if (ex is AssertFailedException || ex is AssertInconclusiveException) throw;

         if (ex.InnerException == null)
         {
             Assert.AreEqual(expected.Message, ex.Message, true, CultureInfo.InvariantCulture);
             Assert.AreEqual(expected.GetType(), ex.GetType());
         }
         else
         {
             Assert.AreEqual(expected.Message, ex.InnerException.Message, true,  CultureInfo.InvariantCulture);
             Assert.AreEqual(expected.GetType(), ex.InnerException.GetType());
         }
      }
   }
}

This would be the testing Foo class:

public class Foo
{
   private readonly int? _number;
   private readonly string _name;

   public Foo(int? number, string name)
   {
      if(!number.HasValue) throw new ArgumentNullException("number");
      if(string.IsNullOrEmpty(name)) throw new ArgumentNullException("name");

      _number = number;
      _name = name;
   }

   public void DoSomething()
   {
      if(_number < 0) throw new ApplicationException("Number should not be negative");
   }

   public void DoSomethingElse(int dividend)
   {
      if(dividend == 0) throw new DivideByZeroException("Number cannot be divided by zero");
   }

   public bool Validate()
   {
      if(_name == "invalid") throw new InvalidCredentialException("Authorization denied");
      return true;
   }

   public Foo GetNewFoo(string newName, int? newNumber, Foo oldFoo)
   {
      if(oldFoo == null) throw new NullReferenceException("Seriously Foo cannot be null");
      return new Foo(newNumber, newName);
   }
}

And this is how it would be used:

[TestMethod]
public void ConstructorThrowsExceptionWhenArgumentsAreInvalid()
{
   CustomAssert.IfExceptionIs(new ArgumentNullException("number"),
      new Func<int?, string, Foo>((num, name) =>; new Foo(num, name)),
      null, "any");

   CustomAssert.IfExceptionIs(new ArgumentNullException("name"),
      new Func<int?, string, Foo>((num, name) => new Foo(num, name)),
      1, string.Empty);
}

[TestMethod]
public void MethodsThrowExceptions()
{
   var firstFoo = new Foo(-5, "something");
   CustomAssert.IfExceptionIs(new ApplicationException("Number should not be negative"),
      new Action(firstFoo.DoSomething));

   var secondFoo = new Foo(5, "something");
   CustomAssert.IfExceptionIs(new DivideByZeroException("Number cannot be divided by zero"),
      new Action<int>(secondFoo.DoSomethingElse),
      0);
}

[TestMethod]
public void FunctionsThrowExceptions()
{
   var foo = new Foo(1, "invalid");

   CustomAssert.IfExceptionIs(new InvalidCredentialException("Authorization denied"),
      new Func<bool>(foo.Validate));

   CustomAssert.IfExceptionIs(new NullReferenceException("Seriously Foo cannot be null"),
      new Func<string, int?, Foo, Foo>(foo.GetNewFoo),
      "any", 3, null);
}

To have the complete picture here are some snippets on how would the tests look like if we were to use the custom attribute:


[TestMethod]
[ExpectedExceptionWithMessage(typeof(ArgumentNullException), "name")]
public void ConstructorThrowsExceptionWhenNameIsEmpty()
{
   new Foo(1, string.Empty);
}

[TestMethod]
[ExpectedExceptionWithMessage(typeof(ApplicationException), "Number should not be negative")]
public void MethodThrowsExceptionWhenNumberIsNegative()
{
   var foo = new Foo(-5, "something");
   foo.DoSomething();
}

Another thing to note is that the implementation I am proposing looks for an inner exception since sometimes the real problem might be hiding in there and the attribute one supports inherited exceptions. Both options could easily be added in either one.

So there you have it, two options to assert the message of an expected exception. Use the one you consider best for you, your team and your project and may your unit tests save you from grief and maintenance nightmares.

Advertisements

Does this make sense to you?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s