Unit test a custom Exception

7k views Asked by At

My code coverage has listed my custom exception as 0 test coverage. I am using MsTest,Moq and Fluentassertions. Is there an appropriate unit test for a custom exception?

Here is my Exception class

  public class ConfigurationNotFoundException : Exception
    {
        public ConfigurationNotFoundException()
        {
        }

        public ConfigurationNotFoundException(string message) : base(message)
        {
        }

        public ConfigurationNotFoundException(string message, Exception innerException) : base(message, innerException)
        {
        }

        protected ConfigurationNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context)
        {
        }
    }
3

There are 3 answers

3
Pierre-Luc Pineault On BEST ANSWER

If it's showing as 0% coverage, it's because it is unused/untested.

Taking your original custom exception class, and this beautiful class which uses it :

public class SomeClass
{
    public void SomeMethod()
    {
        throw new ConfigurationNotFoundException("Hey!");
    }
}

If I have the following Unit Test (with Fluent Assertions) :

[TestClass]
public class SomeClassTest
{
    [TestMethod]
    public void SomeMethodShouldThrow()
    {
        Action invocation = () => new SomeClass().SomeMethod();

        invocation.ShouldThrow<ConfigurationNotFoundException>().WithMessage("Hey!");
    }
}

The coverage shows correctly the used constructor as covered :

Coverage result


So if it's not covered, it is either untested or unused.

To verify if it is unused, you can do a right-click on the method name and select 'Find Usages'. If you have the 'No usages' message, you can safely delete it.

If it is used but untested; test it (the behavior, not the exception class itself).


With our previous example, you'd only need the following :

public class ConfigurationNotFoundException : Exception
{
    public ConfigurationNotFoundException(string message)
        : base(message)
    {
    }
}

The other constructors are not needed to inherit from Exception, and would only clutter the class since they serve no purpose.

2
Jonny Cundall On

You could write a test which calls each constructor, to satisfy your code coverage tool. But you should determine what code needs testing yourself. If you want to have 100% coverage, go ahead and write a crummy unit test, but you are better off looking at why you write tests. Does adding a trivial unit test increase the quality of your code?

3
sfuqua On

Certainly you could write unit tests that call three public constructors. Essentially, your test would be verifying that the constructors do not themselves throw exceptions, and you can verify that the message and innerException are properly stored.

But, this also implies that you are not exercising the code that uses this exception. If you have a MethodA, which can throw this custom exception, then you need to make sure that one of your tests actually causes MethodA to throw the exception.