1 00:00:00,000 --> 00:00:02,290 [No Audio] 2 00:00:02,290 --> 00:00:03,898 In this video we're going to create a brand 3 00:00:03,898 --> 00:00:06,522 new project which uses JUnit and Mockito and we 4 00:00:06,536 --> 00:00:08,263 can use this in the next few videos then 5 00:00:08,263 --> 00:00:10,160 to demonstrate how to use Mockito itself. 6 00:00:10,160 --> 00:00:11,468 So let's jump in. 7 00:00:11,468 --> 00:00:13,359 So first off I'll create a new project. 8 00:00:13,359 --> 00:00:16,649 [No Audio] 9 00:00:16,649 --> 00:00:18,780 We've seen this before, but I'll just do ultra fast. 10 00:00:18,780 --> 00:00:26,530 [No Audio] 11 00:00:26,530 --> 00:00:29,504 Just give it a simple Groupid or Artifactid. 12 00:00:29,504 --> 00:00:30,904 Go to next 13 00:00:30,904 --> 00:00:33,197 [No Audio] 14 00:00:33,197 --> 00:00:34,463 click Finish. 15 00:00:34,463 --> 00:00:38,346 [No Audio] 16 00:00:38,346 --> 00:00:41,306 Just set this to Enable Auto-Import. 17 00:00:41,306 --> 00:00:42,922 Leap up here, 18 00:00:42,922 --> 00:00:46,897 [No Audio] 19 00:00:46,897 --> 00:00:48,314 adding the junit dependency. 20 00:00:48,314 --> 00:00:50,300 [No Audio] 21 00:00:50,300 --> 00:00:51,556 And underneath we're going to 22 00:00:51,556 --> 00:00:53,090 add the Mockito dependency. 23 00:00:53,090 --> 00:00:57,650 [No Audio] 24 00:00:57,650 --> 00:01:00,233 So 3.3.0 is fine for now. 25 00:01:00,233 --> 00:01:02,049 Just tidy that up. 26 00:01:02,049 --> 00:01:06,682 [No Audio] 27 00:01:06,682 --> 00:01:09,016 And if I go across into External Libraries, 28 00:01:09,016 --> 00:01:11,088 we'll see here that we've got junit that 29 00:01:11,088 --> 00:01:14,450 we just included, which is brought in hamcrest. 30 00:01:14,450 --> 00:01:17,566 And we also have Mockito-core:3.3.0 and some 31 00:01:17,566 --> 00:01:19,404 other dependencies it's brought in as well, which 32 00:01:19,404 --> 00:01:22,656 are these bytebuddy ones here and objenesis, those are 33 00:01:22,656 --> 00:01:24,655 basically responsible for doing a little bit of byte 34 00:01:24,655 --> 00:01:27,105 code magic behind the scenes which allows Mockito 35 00:01:27,105 --> 00:01:28,644 to do its thing and to be able to create these 36 00:01:28,644 --> 00:01:31,470 mock objects, programmatically incurred as we'll see. 37 00:01:31,470 --> 00:01:34,070 So anyway, so Mockito Core is the main library 38 00:01:34,070 --> 00:01:36,170 that you want to bring in when you're writing 39 00:01:36,170 --> 00:01:39,182 mock object based unit tests with Mockito. 40 00:01:39,182 --> 00:01:40,166 And once you've done that you'll 41 00:01:40,178 --> 00:01:42,144 be able to access its API. So let's do that. 42 00:01:42,144 --> 00:01:43,222 Now let's just go into the 43 00:01:43,236 --> 00:01:46,610 project structure into src, test, java. 44 00:01:46,610 --> 00:01:48,026 We'll just put a package in here, 45 00:01:48,026 --> 00:01:52,529 [No Audio] 46 00:01:52,529 --> 00:01:53,979 com.javaeasily. 47 00:01:53,979 --> 00:01:56,029 [No Audio] 48 00:01:56,029 --> 00:01:58,810 demos.mockito.myapp 49 00:01:58,810 --> 00:02:03,692 I'll just copy that into the clipboard, so I can create 50 00:02:03,692 --> 00:02:08,616 a corresponding package in the production source 51 00:02:08,616 --> 00:02:12,283 folder as well, which we'll use later when we write 52 00:02:12,283 --> 00:02:14,142 the production classes we're going to test. 53 00:02:14,142 --> 00:02:17,840 And now I'll just put a quick test class in here now, 54 00:02:17,840 --> 00:02:20,660 just so we can see Mockito to check it set up okay. 55 00:02:20,660 --> 00:02:22,656 [No Audio] 56 00:02:22,656 --> 00:02:25,220 I'll just call it AppTest for now. That's fine. 57 00:02:25,220 --> 00:02:26,624 So at this point now we should be able 58 00:02:26,624 --> 00:02:29,164 to use Mockito and at least in buckets API. 59 00:02:29,164 --> 00:02:30,944 Now let's just create a test method and just 60 00:02:30,944 --> 00:02:33,116 try to create a mock object using Mockito just 61 00:02:33,116 --> 00:02:35,026 to make sure that we've got the Mockito API 62 00:02:35,026 --> 00:02:37,040 set up correctly and accessible to us. 63 00:02:37,040 --> 00:02:38,706 So if we jump into here, 64 00:02:38,706 --> 00:02:41,106 [No Audio] 65 00:02:41,106 --> 00:02:46,173 just put a test method in, we'll just call it testIt. 66 00:02:46,173 --> 00:02:48,734 Like I said, the test method is irrelevant 67 00:02:48,734 --> 00:02:50,136 now we're going to delete this anyway. 68 00:02:50,136 --> 00:02:50,988 We just want to make sure that 69 00:02:50,988 --> 00:02:52,260 we've got Mockito set up okay. 70 00:02:52,260 --> 00:02:53,876 And then we can access the API. 71 00:02:53,876 --> 00:02:55,646 So let's just call into Mockito. 72 00:02:55,646 --> 00:02:57,338 You can see here when we type Mockito 73 00:02:57,338 --> 00:02:59,712 intelliJ ideas, give it a suggestion, which is 74 00:02:59,712 --> 00:03:01,598 the class in the org.mockito package. 75 00:03:01,598 --> 00:03:03,382 So org.mockito is the main package for 76 00:03:03,396 --> 00:03:05,628 Mockito and the Mockito class itself is the 77 00:03:05,628 --> 00:03:07,142 main entry point to the framework. 78 00:03:07,142 --> 00:03:10,591 So if we just go Mockito.mock and we can basically 79 00:03:10,591 --> 00:03:13,025 now pass it a class to mock 80 00:03:13,025 --> 00:03:17,484 set calendar from java.util and 81 00:03:17,484 --> 00:03:19,250 we have to give a class reference for that, 82 00:03:19,250 --> 00:03:21,333 [No Audio] 83 00:03:21,333 --> 00:03:24,016 and we get back something that, as far as the 84 00:03:24,016 --> 00:03:26,133 compiler is aware is an instance 85 00:03:26,133 --> 00:03:28,415 of the class time we passed him. 86 00:03:28,415 --> 00:03:33,290 So just put mock calendar = Mockito.mock. 87 00:03:33,290 --> 00:03:36,239 I just put the test annotation in here as well. 88 00:03:36,239 --> 00:03:38,206 And now if we run that 89 00:03:38,206 --> 00:03:43,105 [No Audio] 90 00:03:43,105 --> 00:03:44,606 that's fine and we just put a Breakpoint here and 91 00:03:44,608 --> 00:03:46,228 we'll just see what we've actually got. 92 00:03:46,228 --> 00:03:47,611 So if we Debug this, 93 00:03:47,611 --> 00:03:49,694 [No Audio] 94 00:03:49,694 --> 00:03:51,090 then we can see here mockCalendar. 95 00:03:51,090 --> 00:03:53,822 It's a calendar type but it's actually a 96 00:03:53,836 --> 00:03:56,116 MockitaMock and at this stage we know 97 00:03:56,116 --> 00:03:58,156 that we've set up Mockito correctly and we 98 00:03:58,156 --> 00:04:00,658 can start creating those mock objects done here. 99 00:04:00,658 --> 00:04:03,286 I will explore that further in the next videos.