Consider a function that does some exception handling based on the arguments passed:
List range(start, stop) {
if (start >= stop) {
throw new ArgumentError("start must be less than stop");
}
// remainder of function
}
How do I test that the right kind of exception is raised?
In this case, there are various ways to test the exception. To simply test that an unspecific exception is raised:
to test that the right type of exception is raised:
there are several predefined matchers for general purposes like
throwsArgumentError,throwsRangeError,throwsUnsupportedError, etc.. for types for which no predefined matcher exists, you can useTypeMatcher<T>.to ensure that no exception is raised:
to test the exception type and exception message:
here is another way to do this:
(Thanks to Graham Wheeler at Google for the last 2 solutions).