Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit test a custom Exception

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)
        {
        }
    }
like image 400
MicroMan Avatar asked Sep 13 '25 21:09

MicroMan


1 Answers

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?

like image 81
Jonny Cundall Avatar answered Sep 16 '25 12:09

Jonny Cundall