Search code examples
unit-testingexceptionmstestassert

Test a method throws a specific exception .NET


How to test that a void method throws a specific exception in .NET.

I have a method that throws 3 different types of exceptions depending on inputs. How would I test that I get the current one each time, AND ALSO test that it doesn't throw any when passed with the correct inputs.

Thanks!


Solution

  • If you are using NUnit, you can use the Assert.Throws() method to test for a specific exception being thrown.