1 00:00:00,000 --> 00:00:02,110 [No Audio] 2 00:00:02,110 --> 00:00:03,532 In this video, we're going to continue from the 3 00:00:03,532 --> 00:00:05,399 previous video where we learn how we can use 4 00:00:05,399 --> 00:00:07,899 assert.fail to explicitly fail a test and explore 5 00:00:07,899 --> 00:00:09,994 where we actually might want to use that mechanism. 6 00:00:09,994 --> 00:00:12,116 So let's jump in. Now you might be thinking where is 7 00:00:12,116 --> 00:00:13,162 that going to be useful? 8 00:00:13,162 --> 00:00:15,056 Why would I want to fail a test? 9 00:00:15,056 --> 00:00:16,689 Well, you might want to do that for example, 10 00:00:16,689 --> 00:00:19,172 if you're testing some exception handling. 11 00:00:19,172 --> 00:00:23,088 So you can imagine having a try 12 00:00:23,088 --> 00:00:25,296 [No Audio] 13 00:00:25,296 --> 00:00:26,221 catch, 14 00:00:26,221 --> 00:00:30,162 [No Audio] 15 00:00:30,162 --> 00:00:31,996 some kind of ValidationError e. 16 00:00:31,996 --> 00:00:36,237 [No Audio] 17 00:00:36,237 --> 00:00:39,436 And if we supply bad input, to the system and 18 00:00:39,436 --> 00:00:41,952 the test that we're invoking the test method of. 19 00:00:41,952 --> 00:00:46,010 [No Audio] 20 00:00:46,010 --> 00:00:48,056 Imagine this comment here is replaced with an 21 00:00:48,056 --> 00:00:50,026 actual method call to the class that you're 22 00:00:50,026 --> 00:00:53,169 testing and you specifically give it bad input. If you 23 00:00:53,169 --> 00:00:55,119 give it bad input and you expect a ValidationError 24 00:00:55,119 --> 00:00:57,394 to occur, then you expect that you immediately 25 00:00:57,394 --> 00:00:59,014 then going to end up in the catch block. 26 00:00:59,014 --> 00:01:01,310 Which means that if you end up on this line here, 27 00:01:01,310 --> 00:01:03,371 [No Audio] 28 00:01:03,371 --> 00:01:05,302 then you didn't end up in the catch block. 29 00:01:05,302 --> 00:01:06,586 Which means that the class you're 30 00:01:06,586 --> 00:01:08,998 testing didn't throw a ValidationError. 31 00:01:08,998 --> 00:01:10,592 At that point, you might want to fail the test 32 00:01:10,592 --> 00:01:13,108 and say, something like the validation didn't kick in 33 00:01:13,108 --> 00:01:16,375 or some more descriptive nice error message than that, 34 00:01:16,375 --> 00:01:19,816 and obviously ignore the fact that this is red here. 35 00:01:19,816 --> 00:01:22,186 I've just typed in a random error 36 00:01:22,186 --> 00:01:24,322 that our fictional API might throw. 37 00:01:24,322 --> 00:01:25,748 Actually in fact as well, thinking about it 38 00:01:25,748 --> 00:01:27,584 wouldn't be an error because narrows really like 39 00:01:27,584 --> 00:01:28,988 a stop the world cup type thing. 40 00:01:28,988 --> 00:01:30,472 Instead it would be an exception. 41 00:01:30,472 --> 00:01:32,456 So maybe there's a checked exception which is 42 00:01:32,456 --> 00:01:34,570 thrown out from this API that you've written. 43 00:01:34,570 --> 00:01:36,665 So we're just supposing that here. And at 44 00:01:36,665 --> 00:01:38,516 this point you're probably thinking, but you've just 45 00:01:38,516 --> 00:01:41,097 told me a few seconds ago, that we can put expected 46 00:01:41,097 --> 00:01:43,702 equals and then an exception class that we're expecting 47 00:01:43,702 --> 00:01:45,758 to be thrown and that will make the test pass. 48 00:01:45,758 --> 00:01:47,480 And that's true. You can do that. 49 00:01:47,480 --> 00:01:48,776 And then you're probably thinking, well, in which 50 00:01:48,776 --> 00:01:50,684 case, why would I want to even have 51 00:01:50,684 --> 00:01:52,328 the try catch in the first place? 52 00:01:52,328 --> 00:01:54,514 Why would even want to have this catch block? 53 00:01:54,514 --> 00:01:56,816 And the reason is, because in the catch block you 54 00:01:56,816 --> 00:01:59,230 might want to do some kind of extra assertions. 55 00:01:59,230 --> 00:02:01,282 So for example, if you set things on this validation 56 00:02:01,282 --> 00:02:03,865 exception, if you set different parameters or you 57 00:02:03,865 --> 00:02:06,181 expect the ValidationException to carry some kind of 58 00:02:06,181 --> 00:02:08,299 state with it, then maybe you want to do some 59 00:02:08,299 --> 00:02:10,808 assertions which check, what's been set on it 60 00:02:10,808 --> 00:02:12,814 and what the state is that it's carrying. 61 00:02:12,814 --> 00:02:14,936 So that's one example where you could use that and get 62 00:02:14,936 --> 00:02:16,654 to know how to write better tests with JUnit. 63 00:02:16,654 --> 00:02:18,226 As you get more experience with it, you'll 64 00:02:18,226 --> 00:02:19,832 find other opportunities to use this as well. 65 00:02:19,832 --> 00:02:21,104 But that's just one example which 66 00:02:21,104 --> 00:02:23,040 we wanted to give you. So let's move on.