Testing for Exceptions3:21 with Jeremy McLain
Here how to test that the correct exceptions are thrown.
When unit testing, we don't just want to test that the code works when the right 0:00 data is provided and everything is working as expected. 0:04 This is known as the happy path through the code. 0:08 We also want to make sure that the code handles 0:11 errors gracefully in an expected and predictable way. 0:14 This usually means making sure that the correct exceptions are thrown. 0:17 After all, the interface of a method includes the exceptions that it can throw. 0:21 We need to test the code throws the right exceptions at the right time. 0:26 X unit provides a number of assertions to help test this. 0:29 Let's take a look at the map location class. 0:33 The MapLocation class throws an exception if we try to construct a map location 0:36 that's outside of the map. 0:40 If we only want to generate a test method for 0:42 one method in a class, we can right-click on it and click Create Unit Test. 0:44 Click OK to generate the test class. 0:49 We'll call this method ShouldThrowIfNotOnMap. 0:52 In the arrange step, I'll create a new map object. 1:02 So var map = new Map and we'll just make it a size three by three. 1:05 We'll call the Assert.Throws method 1:14 to test to make sure that it throws the correct exception. 1:19 For the generic parameter we'll give it the type of exception we're expecting. 1:23 So it should be OutOfBoundsException. 1:26 Now we'll pass it a lambda function that executes 1:29 the code that we expect to throw an exception. 1:32 So we'll say new MapLocation with a location that's 1:37 just beyond the bounds of the map, (3, 3) and we'll pass in the map object. 1:42 So this generic parameter here is the type of exception that we expect the code 1:49 inside the lambda to throw. 1:53 If it doesn't throw this exact exception, then the assertion will fail. 1:55 If it passes, then it will return the exception that was thrown. 2:00 We can capture that in a variable, so I'll say var exception. 2:04 This gives us the option to have more assertions that test that the data 2:11 contained in the exception is what's expected. 2:14 You may have noticed that X unit has a lot of different assertion methods. 2:17 We can see a list of them by clicking on the Assert class and 2:21 then hitting F12 on the keyboard. 2:25 This takes us to the definition of the Assert class. 2:28 These asserts are all provided by the X unit that asserts library, 2:30 you can read how to use them by reading their doc comments here. 2:34 As you can see there are search for just about everything you'd want to check, 2:43 you can also write your own assertions. 2:47 And of course, 2:49 if you think there's an assertion that you think should be distributed with X unit, 2:50 you can always submit a pole request to X units GitHub Repo. 2:54 Making custom assertions is a topic for another time though. 2:57 We don't have time in this course to go through examples of using 3:01 all of these assertions, most of them are self-explanatory though. 3:03 So far we've seen how to use the equals and the throws assertions. 3:07 Another common assertion is assert.true, 3:12 which just asserts that whatever passed to it is true. 3:14 We'll get lots more practice calling asserts in this course. 3:18
You need to sign up for Treehouse in order to download course files.Sign up