1 00:00:00,210 --> 00:00:03,900 Now that we've identified every test case, the second step is to start unit testing. 2 00:00:04,140 --> 00:00:08,970 Your first task was to set up a method annotated with before public void setup. 3 00:00:13,040 --> 00:00:18,060 And we're going to give this method of before annotation, remember that when you edited a method with 4 00:00:18,080 --> 00:00:22,910 before, then it's going to run before each test and each test is going to depend on us setting the 5 00:00:22,910 --> 00:00:25,760 cart equal to a new object of the cart class. 6 00:00:31,840 --> 00:00:36,700 Now we can start unit testing, we'll follow the order from requirements dot text, the first test case 7 00:00:36,700 --> 00:00:39,640 tells us to check if the store contains an item after it's been added. 8 00:00:42,160 --> 00:00:44,950 So I'll create a unit test named item added test. 9 00:00:55,120 --> 00:01:00,550 And here I need to research the story contains an item that was added and checking if a store contains 10 00:01:00,550 --> 00:01:03,010 an item, depends on us adding the item first. 11 00:01:03,370 --> 00:01:06,640 So we'll do that inside the before method carport add. 12 00:01:12,390 --> 00:01:13,470 Will add a new item. 13 00:01:16,730 --> 00:01:18,170 Pepsi won ninety nine. 14 00:01:24,560 --> 00:01:30,440 And we'll add another item to the shopping cart cart that add new item, Krush, also 199. 15 00:01:36,730 --> 00:01:39,040 All right, but the store doesn't have an ad method. 16 00:01:40,670 --> 00:01:44,330 So we need to go to Kadirgamar public void add. 17 00:01:54,240 --> 00:01:58,110 And the before method implies that ad should receive an object of the item class. 18 00:02:04,810 --> 00:02:09,169 And here, I guess I'll just have to add a new copy of the item object inside the array list. 19 00:02:10,039 --> 00:02:10,570 OK. 20 00:02:11,940 --> 00:02:17,050 And now how do we make sure the aid method is working as it should inside the item added unit test? 21 00:02:17,070 --> 00:02:20,760 We need to check if the cart contains the item object that was added. 22 00:02:21,150 --> 00:02:22,740 So we're going to use a true. 23 00:02:25,520 --> 00:02:28,340 To check if the cart contains the crushed object. 24 00:02:38,080 --> 00:02:43,900 This test asserts the cart contains a certain item, and now inside Staudt Java, we need to write code 25 00:02:43,900 --> 00:02:48,580 to make the test fail, something to create a public boolean method called contains. 26 00:02:52,280 --> 00:02:56,100 And here, the unit test implies that our method needs to receive an item object. 27 00:02:56,600 --> 00:03:00,830 Again, it's kind of nice that we're writing our code based on how we set up our tests. 28 00:03:01,310 --> 00:03:05,840 And here the only thing we can do is return a boolean that checks if this items. 29 00:03:10,170 --> 00:03:12,330 That contains the item parameter. 30 00:03:16,200 --> 00:03:17,780 All right, our test should fail. 31 00:03:25,210 --> 00:03:29,650 Which is fine, because that's the first step to writing a unit test and it says that it was expecting 32 00:03:29,650 --> 00:03:31,690 true, but our unit test returns false. 33 00:03:31,960 --> 00:03:35,680 So this is where you ask yourself, OK, why is the test failing? 34 00:03:38,480 --> 00:03:39,770 If you put a breakpoint here. 35 00:03:46,770 --> 00:03:51,390 You can see that the cart already contains two objects courtesy of our before method. 36 00:03:53,680 --> 00:03:59,680 The objects are equal in terms of state, their fields are equal, but remember that contains uses the 37 00:03:59,680 --> 00:04:02,470 default equals method to compare item objects. 38 00:04:02,890 --> 00:04:06,250 So it compares the reference of each item against the item parameter. 39 00:04:06,640 --> 00:04:11,510 The reference that we're passing in isn't equal to any of the references inside the array list. 40 00:04:11,890 --> 00:04:17,110 So ultimately, Java determines that the aerialist doesn't contain the object being passed in. 41 00:04:20,630 --> 00:04:26,510 But if we create our own equals method from just the item class then contains is going to use that one 42 00:04:26,510 --> 00:04:27,050 instead. 43 00:04:29,770 --> 00:04:33,280 So inside the item class, we're going to customize the equals method. 44 00:04:39,560 --> 00:04:41,540 The equals method needs to return a boolean. 45 00:04:44,880 --> 00:04:48,820 And receives an object, what's the type of that object we don't know. 46 00:04:48,840 --> 00:04:49,920 It can be any object. 47 00:04:50,430 --> 00:04:53,910 First, we're going to check if the object that we're comparing against is No. 48 00:04:55,230 --> 00:04:59,760 Because if it happens to be null, then there's no way it equals the item object that's calling this 49 00:04:59,760 --> 00:05:06,390 method, let's assume the object is it now then we need to check if it isn't of type item if the object 50 00:05:06,390 --> 00:05:08,400 isn't an instance of the item class. 51 00:05:12,220 --> 00:05:13,420 Then return false. 52 00:05:17,650 --> 00:05:23,050 OK, so at this point, the object, is it no, it is an instance of item, so we can typecast it to 53 00:05:23,050 --> 00:05:23,740 type item. 54 00:05:29,080 --> 00:05:33,970 And finally, we got to check if the objects fields are equal to the current object that's calling this 55 00:05:33,970 --> 00:05:34,390 method. 56 00:05:37,280 --> 00:05:42,320 We're going to compare the name and price from the object that's calling this method to the one that's 57 00:05:42,320 --> 00:05:43,760 being passed in as a parameter. 58 00:05:56,030 --> 00:05:57,650 All right, let's run the test now. 59 00:06:09,360 --> 00:06:10,260 And it passes. 60 00:06:25,220 --> 00:06:30,770 Here you can visualize it, the contains method is using your equals method to compare your object against 61 00:06:30,770 --> 00:06:32,150 each item in the aerialists. 62 00:06:32,450 --> 00:06:35,170 This points to the current item that's called the equals. 63 00:06:35,180 --> 00:06:36,380 And here is the parameter. 64 00:06:39,040 --> 00:06:40,360 The parameter is not known. 65 00:06:41,450 --> 00:06:42,710 It is an instance of item. 66 00:06:49,240 --> 00:06:53,380 But the fields aren't equal, so equals returns, falls for the first element. 67 00:06:59,070 --> 00:07:02,100 And if you keep going, the second time equals gets called. 68 00:07:03,110 --> 00:07:04,490 This parameter is not known. 69 00:07:04,520 --> 00:07:05,840 It is an instance of item. 70 00:07:08,480 --> 00:07:11,150 And the fields are equal, so equals returns, true. 71 00:07:16,900 --> 00:07:22,690 And because equal is return, true contains determines that the Israelis does contain that object and 72 00:07:22,690 --> 00:07:25,600 it returns true as well and our unit test passes. 73 00:07:29,730 --> 00:07:34,470 All right, since the unit test passes, we can rest assured that our code doesn't have any bugs. 74 00:07:37,200 --> 00:07:38,520 But can I be refactored? 75 00:07:39,030 --> 00:07:40,360 The answer is no. 76 00:07:40,380 --> 00:07:42,390 The code is as elegant as it's going to be. 77 00:07:44,830 --> 00:07:49,990 The second test case tells us the card should skip a duplicate item, so I'll create a unit test named 78 00:07:49,990 --> 00:07:50,980 SIP's Duplicate. 79 00:08:01,200 --> 00:08:06,510 And here I'll make an assertion using a certain false assert, false expects the value to be false. 80 00:08:10,110 --> 00:08:14,340 And we're expecting the odd method to return false if we add a duplicate item. 81 00:08:31,640 --> 00:08:33,200 So back in Córdova. 82 00:08:37,000 --> 00:08:40,809 We have to write code to make the test fail, so I'm going to return true. 83 00:08:53,520 --> 00:08:55,410 Our test should fail, which is fine. 84 00:08:59,610 --> 00:09:04,650 It says that it was expecting faults, but our unit test returns true, so back in Car Java, we got 85 00:09:04,650 --> 00:09:06,390 to write code to make the test pass. 86 00:09:06,390 --> 00:09:10,650 We'll say if items that contains the item being passed in. 87 00:09:16,000 --> 00:09:16,990 Return false. 88 00:09:18,570 --> 00:09:22,050 Otherwise, we'll return true and add that item to the aerialists. 89 00:09:26,860 --> 00:09:33,730 OK, rerun the test and it passes so we can rest assured that our code doesn't have any bugs, but can 90 00:09:33,730 --> 00:09:34,810 it be a refactored? 91 00:09:34,870 --> 00:09:36,220 The answer is no. 92 00:09:36,280 --> 00:09:38,320 The code is as elegant as it's going to be.