1 00:00:00,000 --> 00:00:01,690 [No Audio] 2 00:00:01,690 --> 00:00:03,886 So Mockito is a library that you can include 3 00:00:03,886 --> 00:00:05,806 and use in the unit tests of your Java 4 00:00:05,806 --> 00:00:08,096 projects to enable you to create various types of 5 00:00:08,096 --> 00:00:11,189 so called testables which your tests can use. 6 00:00:11,189 --> 00:00:13,966 Now there are different types of testables, and you'll 7 00:00:13,966 --> 00:00:16,978 hear words like dummy, stub, mock, spy, and fake. 8 00:00:16,978 --> 00:00:18,872 And don't worry, we'll cover what these are later 9 00:00:18,872 --> 00:00:20,888 in the course, but for now we're just going 10 00:00:20,888 --> 00:00:22,724 to refer to them as mock objects in this 11 00:00:22,724 --> 00:00:25,694 little intro video to keep things nice and simple. 12 00:00:25,694 --> 00:00:28,940 So these Mock objects which Mockito lets you create. 13 00:00:28,940 --> 00:00:31,489 You can think of these as stand-ins for real objects 14 00:00:31,489 --> 00:00:34,573 which you can use in your tests. When you write test 15 00:00:34,573 --> 00:00:37,186 code, specifically when you write a unit test, you're 16 00:00:37,186 --> 00:00:39,596 really interested in testing the unit that's the 17 00:00:39,596 --> 00:00:41,698 class you're testing and nothing else. 18 00:00:41,698 --> 00:00:44,146 That means that in your unit test, the only real 19 00:00:44,146 --> 00:00:46,412 production code which executes should be this class 20 00:00:46,412 --> 00:00:48,872 you're writing the test for, and that class will 21 00:00:48,872 --> 00:00:51,236 work with the standings these mock objects, as it 22 00:00:51,236 --> 00:00:52,846 would do in the real world. 23 00:00:52,846 --> 00:00:54,980 This is where Mockito comes in. 24 00:00:54,980 --> 00:00:58,064 It lets us create these mock objects super easily and 25 00:00:58,064 --> 00:01:00,460 lets us set up how they behave in our tests. 26 00:01:00,460 --> 00:01:02,504 Doing this allows you to write really good 27 00:01:02,504 --> 00:01:04,891 unit tests which can test your application much 28 00:01:04,891 --> 00:01:07,537 more comprehensively and quicker than before. 29 00:01:07,537 --> 00:01:09,992 Let's think about a web app for a second. 30 00:01:09,992 --> 00:01:12,242 It might have a LoginController which can take the 31 00:01:12,242 --> 00:01:14,525 username and password entered by the user on a login 32 00:01:14,525 --> 00:01:17,041 screen and return a view of the homepage for logged in 33 00:01:17,041 --> 00:01:19,925 user if the password is good, or return the user back 34 00:01:19,925 --> 00:01:22,667 to the login screen if the password is bad. 35 00:01:22,667 --> 00:01:25,832 So here's our LoginController, waiting for those 36 00:01:25,832 --> 00:01:28,364 username and password combinations to come in. 37 00:01:28,364 --> 00:01:30,158 Now, to do its job, you will probably 38 00:01:30,158 --> 00:01:32,758 need to delegate to some service. So let's say 39 00:01:32,758 --> 00:01:35,358 it has a reference to an authentication service. 40 00:01:35,358 --> 00:01:36,882 In reality, they'd probably be a 41 00:01:36,896 --> 00:01:38,914 repository too, to access the database. 42 00:01:38,914 --> 00:01:40,856 But let's leave that aside for now. 43 00:01:40,856 --> 00:01:43,184 The point is that in production, when the app 44 00:01:43,184 --> 00:01:45,392 is deployed and running, these two classes work 45 00:01:45,392 --> 00:01:48,080 together to get the user logged in or kicking out 46 00:01:48,080 --> 00:01:50,230 with the login controller processing the web request, 47 00:01:50,230 --> 00:01:52,780 but delegating to the authentication service to do the 48 00:01:52,780 --> 00:01:55,813 actual real work of verifying the user's credentials. 49 00:01:55,813 --> 00:01:58,904 So thinking about the testing aspect, when I want to 50 00:01:58,904 --> 00:02:01,136 test the LoginController, when I want to write a 51 00:02:01,136 --> 00:02:04,064 test for it, a unit test for it. What I want to have is 52 00:02:04,064 --> 00:02:05,774 the real LoginController itself. 53 00:02:05,774 --> 00:02:08,576 It's what I'm testing, so it needs to be the real deal. 54 00:02:08,576 --> 00:02:10,294 But I want to have a mock 55 00:02:10,294 --> 00:02:12,770 Authentication Service that it works with. 56 00:02:12,770 --> 00:02:14,144 That's because the scope of my 57 00:02:14,144 --> 00:02:15,778 unit test is just the unit. 58 00:02:15,778 --> 00:02:17,144 It's just the thing I'm testing, which 59 00:02:17,144 --> 00:02:18,946 in this case it's a LoginController. 60 00:02:18,946 --> 00:02:20,399 So I don't want anything else to be in 61 00:02:20,399 --> 00:02:22,876 that test that's real, only the controller. 62 00:02:22,876 --> 00:02:25,268 But my controller still needs a service to work with. 63 00:02:25,268 --> 00:02:27,536 So I create a mock Authentication Service that 64 00:02:27,536 --> 00:02:29,558 is a stand in for the real thing. 65 00:02:29,558 --> 00:02:31,606 Now, having a mock object courtesy 66 00:02:31,606 --> 00:02:33,590 of Mockito in place is awesome. 67 00:02:33,590 --> 00:02:36,082 It means I can put the real code through its paces. 68 00:02:36,082 --> 00:02:38,086 In other words, test all sorts of scenarios 69 00:02:38,086 --> 00:02:40,060 to see how my production code will behave. 70 00:02:40,060 --> 00:02:41,888 Maybe in the first test, I set up 71 00:02:41,888 --> 00:02:43,952 the mock service to behave nicely and return 72 00:02:43,952 --> 00:02:45,698 that the user's password is correct. 73 00:02:45,698 --> 00:02:47,926 Maybe in the second test I set up the mock 74 00:02:47,926 --> 00:02:51,032 to misbehave and throw an exception and in the third 75 00:02:51,032 --> 00:02:53,348 test I might return an empty user object. 76 00:02:53,348 --> 00:02:55,196 And there are other tests too which I could write. 77 00:02:55,196 --> 00:02:57,034 The point is that I can see how my production 78 00:02:57,034 --> 00:02:59,392 code behaves through different scenarios in a way 79 00:02:59,392 --> 00:03:01,724 that's quick to set up, develop and fun to use. 80 00:03:01,724 --> 00:03:04,052 And here I might even fix up the production code 81 00:03:04,052 --> 00:03:05,924 while I'm at it, as I learned more about my 82 00:03:05,924 --> 00:03:08,062 code than I initially thought about when I was writing 83 00:03:08,062 --> 00:03:10,894 it and I'll have time to do this since Mockito 84 00:03:10,894 --> 00:03:13,076 is quick to use and code with. 85 00:03:13,076 --> 00:03:15,080 So this is what Mockito is all about. 86 00:03:15,080 --> 00:03:17,384 It's about helping you write better unit tests through 87 00:03:17,384 --> 00:03:20,113 offering a nice and easy to use API for creating, 88 00:03:20,113 --> 00:03:23,013 manipulating and even validating these mock objects. 89 00:03:23,013 --> 00:03:26,216 Specifically, it's easy to set up and install offers an 90 00:03:26,216 --> 00:03:28,432 easy way to create different types of test doubles 91 00:03:28,432 --> 00:03:31,132 those mock objects we've just seen, as an easy to use 92 00:03:31,132 --> 00:03:33,699 API for stubbing behavior that's programming the mocks 93 00:03:33,699 --> 00:03:35,782 to do what you want them to do in your tests. 94 00:03:35,782 --> 00:03:38,264 It's also got an API for verifying interactions on 95 00:03:38,264 --> 00:03:40,618 mocks two which lets you check that the collaborators 96 00:03:40,618 --> 00:03:43,484 of your production class received the right calls with 97 00:03:43,484 --> 00:03:46,276 the right parameters and return the right responses. 98 00:03:46,276 --> 00:03:48,776 And finally it's got a noninvasive API so 99 00:03:48,776 --> 00:03:51,286 it's easy to apply to real world projects. 100 00:03:51,286 --> 00:03:52,508 So there it is. 101 00:03:52,508 --> 00:03:54,610 You now have a high level overview of Mockito 102 00:03:54,610 --> 00:03:56,324 and I'll see you in the next video to 103 00:03:56,324 --> 00:03:58,052 show you exactly how we can use it.