1 00:00:00,180 --> 00:00:04,200 Now that we've identified every test case, the second step is to start unit testing. 2 00:00:06,540 --> 00:00:09,750 Your first task was to set up avoid method named set up. 3 00:00:11,450 --> 00:00:13,250 Public void setup. 4 00:00:16,020 --> 00:00:18,690 And we're going to give this method a before annotation. 5 00:00:20,210 --> 00:00:26,090 When you annotate a method with before it's going to run, before each test in this method is where 6 00:00:26,090 --> 00:00:31,760 you would initialize the variables that each test is going to depend on in this case, each test is 7 00:00:31,760 --> 00:00:36,620 going to depend on us setting the store equal to a new object of the store class. 8 00:00:38,880 --> 00:00:43,680 OK, now we can start, you know, testing, we're going to follow the order from requirements dot text. 9 00:00:44,160 --> 00:00:48,900 The first test case tells us to check if the story contains a movie after it's been added. 10 00:00:50,100 --> 00:00:53,880 So I'll create a unit test named the added test. 11 00:01:01,580 --> 00:01:03,320 Public void movie added. 12 00:01:10,670 --> 00:01:16,970 And checking if his store contains a movie depends on us adding the movie first, so we'll do that inside 13 00:01:16,970 --> 00:01:17,930 the before method. 14 00:01:19,450 --> 00:01:22,780 Stalked and movie will add a new movie. 15 00:01:28,860 --> 00:01:30,960 And it's going to be The Shawshank Redemption. 16 00:01:40,080 --> 00:01:40,710 Blu ray. 17 00:01:45,970 --> 00:01:46,990 Nine point to. 18 00:01:54,540 --> 00:01:57,270 We'll do the same thing here and The Godfather. 19 00:02:11,520 --> 00:02:15,030 The store doesn't have an ad method, so we need to go to store Java. 20 00:02:21,140 --> 00:02:23,060 Public void at movie. 21 00:02:28,790 --> 00:02:33,650 And the before method implies that ad movie should receive an object of the movie class. 22 00:02:38,720 --> 00:02:43,610 And here, I guess I'll just have to add a new copy of the movie Object Inside the Aerialists. 23 00:02:51,830 --> 00:02:55,270 OK, now how do we make sure the odd movie method is working as it should? 24 00:02:57,690 --> 00:03:02,580 Inside the movie and on unit test, we need to check if the story contains the movie object that was 25 00:03:02,580 --> 00:03:02,910 added. 26 00:03:04,060 --> 00:03:05,680 We're going to use a true. 27 00:03:13,860 --> 00:03:16,410 To check if the store contains The Godfather. 28 00:03:27,080 --> 00:03:31,370 We're going to assert the store contains the movie after adding its. 29 00:03:34,540 --> 00:03:40,450 And now inside Staudt Java, we need to write code to make the test fail, I'll create a public boolean 30 00:03:40,450 --> 00:03:42,070 method called Contains. 31 00:03:45,960 --> 00:03:49,920 And the unit test implies that our method needs to receive a movie object. 32 00:03:51,760 --> 00:03:56,590 And it's really nice that our unit test is basically telling us how to write our code, and here the 33 00:03:56,590 --> 00:04:01,990 only thing we can do is return a boolean that checks if this movie contains the movie parameter. 34 00:04:12,750 --> 00:04:14,270 OK, our test should fail. 35 00:04:19,050 --> 00:04:24,480 Which is fine, because that's the first step to writing a unit test, so it says that it was expecting 36 00:04:24,480 --> 00:04:26,840 true, but our unit test returns false. 37 00:04:27,510 --> 00:04:32,370 And this is where you ask yourself, why is my test failing if you put a breakpoint here? 38 00:04:41,860 --> 00:04:47,020 You can see that the store already contains two objects courtesy of our before method. 39 00:04:57,510 --> 00:04:58,920 Well, step inside the function. 40 00:05:02,300 --> 00:05:08,420 But remember, that contains uses the default equals method to compare movie objects, so it compares 41 00:05:08,420 --> 00:05:11,210 the reference of each movie against the movie parameter. 42 00:05:11,750 --> 00:05:16,260 The reference for passing in isn't equal to any of the references inside the array list. 43 00:05:16,610 --> 00:05:21,830 So ultimately contains determines that the area list doesn't contain the object being passed in. 44 00:05:27,640 --> 00:05:34,000 But if we create our own equals method for objects of the movie class and contains is going to use that 45 00:05:34,000 --> 00:05:34,660 one instead. 46 00:05:36,740 --> 00:05:39,890 So inside the movie class, we're going to customize the equals method. 47 00:05:46,020 --> 00:05:51,940 The equals method needs to return a boolean and receives an object, what's the type of that object? 48 00:05:51,960 --> 00:05:52,760 We don't know. 49 00:05:52,770 --> 00:05:54,030 It can be any object. 50 00:05:57,310 --> 00:06:01,510 But first, we're going to check if the object that we're comparing against is No. 51 00:06:02,960 --> 00:06:07,790 Because if it happens to be null, then there's no way it equals the movie object that's calling this 52 00:06:07,790 --> 00:06:10,460 method, otherwise it would have thrown a null pointer exception. 53 00:06:11,710 --> 00:06:16,750 And assuming that the movie object, is it no, we need to check if it isn't of type movie. 54 00:06:18,680 --> 00:06:22,850 And if the object isn't an instance of the movie class, we'll return false. 55 00:06:26,980 --> 00:06:32,770 OK, so at this point, the object, is it no, it is an instance of movie, so we can typecast it to 56 00:06:32,770 --> 00:06:33,520 type movie. 57 00:06:39,400 --> 00:06:44,500 And finally, we need to check if the objects fields are equal to the current object that's calling 58 00:06:44,500 --> 00:06:49,890 this method, we need to compare the name format, rating, selling price range, price and availability. 59 00:06:49,900 --> 00:06:54,430 We need to check if its fields are equal to the current object that's calling this method. 60 00:07:57,010 --> 00:07:58,180 And it passes. 61 00:08:17,700 --> 00:08:23,100 Here you can visualize that the contains method is using your equals method to compare each movie in 62 00:08:23,100 --> 00:08:23,880 the array list. 63 00:08:32,880 --> 00:08:37,110 This points to the current movie that's calling equals, and here's our parameter. 64 00:08:38,970 --> 00:08:40,409 The parameter is not no. 65 00:08:45,490 --> 00:08:47,020 It is an instance of movie. 66 00:08:48,360 --> 00:08:50,130 But their fields aren't equal. 67 00:08:55,450 --> 00:08:58,120 So equals returns false for the first element. 68 00:09:00,240 --> 00:09:01,380 And if you keep going. 69 00:09:07,460 --> 00:09:08,840 The parameter is not no. 70 00:09:11,230 --> 00:09:12,730 It is an instance of movie. 71 00:09:18,940 --> 00:09:22,630 And their fields are equal, so equal returns, true. 72 00:09:29,110 --> 00:09:33,220 And because equals returns true, contains returns true as well. 73 00:09:37,040 --> 00:09:38,720 So our unit test passes. 74 00:09:44,930 --> 00:09:50,300 All right, since the unit test passes, we can rest assured that our code doesn't have any bugs, but 75 00:09:50,480 --> 00:09:51,770 can it be refactored? 76 00:09:54,730 --> 00:09:57,760 No, because the code is as elegant as it's going to be.