1 00:00:00,000 --> 00:00:02,590 [No Audio] 2 00:00:02,590 --> 00:00:03,934 So in this video we're going to explore 3 00:00:03,934 --> 00:00:05,828 what test failure looks like in the IDE. 4 00:00:05,828 --> 00:00:07,354 And that's when a test fails. 5 00:00:07,354 --> 00:00:08,984 So as we saw in the previous video, when 6 00:00:08,984 --> 00:00:11,456 a test passes and is successful, we get green, 7 00:00:11,456 --> 00:00:13,853 but we can also have red as well. So for 8 00:00:13,853 --> 00:00:17,968 example here, if I simulate an exception being thrown 9 00:00:17,968 --> 00:00:25,316 [No Audio] 10 00:00:25,316 --> 00:00:26,633 and then rerun, 11 00:00:26,633 --> 00:00:29,700 [No Audio] 12 00:00:29,700 --> 00:00:33,534 you can see here we now have red, and the test failed. 13 00:00:33,534 --> 00:00:36,897 It says the test failed, 1 of 1 test failed. And 14 00:00:36,897 --> 00:00:40,197 the reason being, is that we have this RuntimeException 15 00:00:40,197 --> 00:00:42,090 here and this would be a full stack trace 16 00:00:42,090 --> 00:00:44,506 which would enable you to pinpoint the exact point 17 00:00:44,506 --> 00:00:46,756 where the unit test failure occurred and the sequence 18 00:00:46,756 --> 00:00:49,392 of method calls which led up to that test failure. 19 00:00:49,392 --> 00:00:51,366 Now the reason for this is because we didn't 20 00:00:51,366 --> 00:00:54,798 set this test up to expect a RuntimeException. 21 00:00:54,798 --> 00:00:57,570 So JUnit takes the reasonably fair approach 22 00:00:57,570 --> 00:00:59,404 of viewing an exception which occurs during 23 00:00:59,404 --> 00:01:01,446 its execution as a test failure. 24 00:01:01,446 --> 00:01:04,013 However, if you had a test in which you specifically 25 00:01:04,013 --> 00:01:06,879 wanted to ensure that an exception must run, 26 00:01:06,879 --> 00:01:09,163 for example supposing, you're testing some validation 27 00:01:09,163 --> 00:01:11,330 and you're testing the validation routine with 28 00:01:11,330 --> 00:01:13,296 bad input, then because you're testing the 29 00:01:13,296 --> 00:01:15,016 validation routine with bad input, you 30 00:01:15,016 --> 00:01:16,465 would expect that the class that you're testing 31 00:01:16,465 --> 00:01:18,432 would throw some kind of validation exception 32 00:01:18,432 --> 00:01:19,842 back out to the user. 33 00:01:19,842 --> 00:01:21,232 And so in that case you'd want to tell 34 00:01:21,232 --> 00:01:23,790 JUnit they are actually expecting an exception. 35 00:01:23,790 --> 00:01:25,864 And you can do this, by using an 36 00:01:25,864 --> 00:01:28,255 addon to @Test or rather an extra parameter 37 00:01:28,255 --> 00:01:30,360 to the test annotation which is expected. 38 00:01:30,360 --> 00:01:33,610 So if you put expected equals under the class, 39 00:01:33,610 --> 00:01:37,893 [No Audio] 40 00:01:37,893 --> 00:01:40,292 then this signifies to JUnit that during that test 41 00:01:40,292 --> 00:01:41,876 it's expecting that it's going to get a runtime 42 00:01:41,876 --> 00:01:44,326 exception and that's a good thing and therefore it will 43 00:01:44,326 --> 00:01:46,892 pass the test. So if I ruin this now 44 00:01:46,892 --> 00:01:51,352 [No Audio] 45 00:01:51,352 --> 00:01:53,574 you can see here the test passes once again. 46 00:01:53,574 --> 00:01:55,944 Of course this throw new RuntimeException is 47 00:01:55,944 --> 00:01:57,696 just contrived and we will never put this 48 00:01:57,696 --> 00:01:59,652 snippet of code inside a unit test. 49 00:01:59,652 --> 00:02:01,082 Rather this exception will be thrown 50 00:02:01,082 --> 00:02:02,846 out from the clash of testing. 51 00:02:02,846 --> 00:02:04,966 But nevertheless I wanted to show you that just to 52 00:02:04,968 --> 00:02:07,187 tell you about the expected annotation parameter to 53 00:02:07,187 --> 00:02:08,788 test. Since that's the way that we can actually 54 00:02:08,788 --> 00:02:10,503 evaluate that we're getting back the exceptions 55 00:02:10,503 --> 00:02:13,104 that we expect if the API we're testing is designed 56 00:02:13,104 --> 00:02:15,462 to them back under that particular test case. 57 00:02:15,462 --> 00:02:17,450 But anyway, now you know about test failure 58 00:02:17,450 --> 00:02:19,126 and exceptions in Junit, we should be in 59 00:02:19,128 --> 00:02:21,476 a good position now to move forward.