1 00:00:00,000 --> 00:00:02,290 [No Audio] 2 00:00:02,290 --> 00:00:04,450 In this video, we're going to look at JUnits Assertions 3 00:00:04,450 --> 00:00:07,033 API, which it gives us to get a bit of a feel for the 4 00:00:07,033 --> 00:00:09,133 kinds of assertions which we can do in our code. 5 00:00:09,133 --> 00:00:10,532 So let's jump in. So 6 00:00:10,532 --> 00:00:11,816 picking up from where we were in the 7 00:00:11,816 --> 00:00:14,077 last video, if we just get rid of that now, 8 00:00:14,077 --> 00:00:17,269 since we're talking about assertions, I should 9 00:00:17,269 --> 00:00:19,052 dig into these a little bit further. 10 00:00:19,052 --> 00:00:23,027 So JUnit has an assert class. 11 00:00:23,027 --> 00:00:25,784 So if I type Assert and then ., you 12 00:00:25,784 --> 00:00:28,200 can see here there are a ton of different methods 13 00:00:28,200 --> 00:00:31,066 which enable you to assert different things, courtesy 14 00:00:31,066 --> 00:00:32,734 of JUnits Assertions API. 15 00:00:32,734 --> 00:00:34,148 So in general you can assert things, but you 16 00:00:34,148 --> 00:00:36,488 can also fail tests like we've seen before. 17 00:00:36,488 --> 00:00:38,228 But focusing on the assertion side, you 18 00:00:38,228 --> 00:00:39,706 can assert that things are not null. 19 00:00:39,706 --> 00:00:41,439 So in other words, like an object reference 20 00:00:41,439 --> 00:00:43,414 is present in a variable. So it's useful 21 00:00:43,414 --> 00:00:45,162 for example, if you call a method and 22 00:00:45,176 --> 00:00:47,096 you expect an object to be returned, you 23 00:00:47,096 --> 00:00:48,658 might want to check that it's not null. 24 00:00:48,658 --> 00:00:51,557 You can check that some boolean condition is true. 25 00:00:51,557 --> 00:00:54,748 You can check that two arrays are equal. You can 26 00:00:54,748 --> 00:00:56,765 see here they're overloaded for different types 27 00:00:56,765 --> 00:00:59,806 [No Audio] 28 00:00:59,806 --> 00:01:02,172 pointing the two objects are equal, and again 29 00:01:02,172 --> 00:01:04,044 they are overloaded methods, so you can feel 30 00:01:04,044 --> 00:01:06,278 free to check those out in your own time. 31 00:01:06,278 --> 00:01:08,842 But just note that in general, these methods tend to 32 00:01:08,856 --> 00:01:10,958 come in pairs, so you can have assertEquals, 33 00:01:10,958 --> 00:01:13,178 and then there'll be an assertNotEquals, or there's 34 00:01:13,178 --> 00:01:15,446 an assertTrue, then there's an assertFalse. 35 00:01:15,446 --> 00:01:17,400 And broadly, what these allow you to check 36 00:01:17,400 --> 00:01:19,970 for, are basically (not clear) of object references. 37 00:01:19,970 --> 00:01:22,453 Equality of objects and values. Actually 38 00:01:22,453 --> 00:01:24,636 speaking of equality as well, you'll notice here that 39 00:01:24,636 --> 00:01:27,276 we have the first argument is the expected thing that 40 00:01:27,276 --> 00:01:30,600 we expect, and the second argument is the actual object 41 00:01:30,600 --> 00:01:33,198 that we get back, which we'll see in a second. 42 00:01:33,198 --> 00:01:36,134 We also have assertions of as many equal 43 00:01:36,134 --> 00:01:37,644 to and that kind of thing. 44 00:01:37,644 --> 00:01:39,324 So the best thing to do now is to look 45 00:01:39,324 --> 00:01:41,076 into your project, which you have set up in the 46 00:01:41,076 --> 00:01:43,140 previous section, and just do what I did. Just do 47 00:01:43,140 --> 00:01:46,306 Assert. after you've imported the org.junit.assert 48 00:01:46,306 --> 00:01:48,223 class and just go through those methods and really 49 00:01:48,223 --> 00:01:50,076 take them in and have a bit of a play around and 50 00:01:50,076 --> 00:01:51,228 get a feel for how they work. 51 00:01:51,228 --> 00:01:52,572 And in this way you get to know them 52 00:01:52,572 --> 00:01:54,100 first hand as you get to use them.