1 00:00:00,000 --> 00:00:02,590 [No Audio] 2 00:00:02,590 --> 00:00:04,112 In this video we're going to start to think 3 00:00:04,112 --> 00:00:06,694 about how we can code up a layered architecture. 4 00:00:06,694 --> 00:00:08,204 So in other words, an architecture with 5 00:00:08,204 --> 00:00:09,956 respects the three layers you have in 6 00:00:09,956 --> 00:00:12,488 a web application, that's presentation layer, service 7 00:00:12,488 --> 00:00:14,878 layer, and a data or persistence layer. 8 00:00:14,878 --> 00:00:16,771 And what we're going to do with this, is 9 00:00:16,771 --> 00:00:19,184 it will enable us to describe basically how 10 00:00:19,184 --> 00:00:21,416 we approach testing with mock objects, how we 11 00:00:21,416 --> 00:00:23,566 apply that to this layered architecture. 12 00:00:23,566 --> 00:00:25,726 So the next few videos might not seem so relevant 13 00:00:25,726 --> 00:00:28,076 to unit testing, but they are because it's important to 14 00:00:28,076 --> 00:00:31,083 understand how the lead architecture is coded, because 15 00:00:31,083 --> 00:00:33,223 then we can explain how we apply Mockito to that. 16 00:00:33,223 --> 00:00:35,072 So just hang in there for these next few videos. 17 00:00:35,072 --> 00:00:36,536 Bear with us while we cover this up, 18 00:00:36,536 --> 00:00:38,434 and then we'll pick up the Mockito testing 19 00:00:38,434 --> 00:00:40,784 aspect of it in a few videos time. 20 00:00:40,784 --> 00:00:43,006 So let's jump in now, and look at the controller. 21 00:00:43,006 --> 00:00:44,564 So we've got our project set up now, 22 00:00:44,564 --> 00:00:46,846 we can use Mockito and we've got JUnit installed. 23 00:00:46,846 --> 00:00:49,575 Let's just remove this code here because 24 00:00:49,575 --> 00:00:51,644 we don't really need this. That was just a test, 25 00:00:51,644 --> 00:00:53,827 we had everything set up okay. 26 00:00:53,827 --> 00:00:56,510 that's gone. Now it's plugging 27 00:00:56,510 --> 00:00:57,874 a couple of classes which interact 28 00:00:57,874 --> 00:01:00,536 with each other that we can demonstrate Mockito with. 29 00:01:00,536 --> 00:01:03,154 So if I go into here in the production source 30 00:01:03,154 --> 00:01:07,480 folder here and we'll just create a LoginController. 31 00:01:07,480 --> 00:01:11,170 So let's suppose this LoginController is responsible 32 00:01:11,170 --> 00:01:13,460 for serving web requests in a web application. 33 00:01:13,460 --> 00:01:16,724 So Http request comes in which passes the username and 34 00:01:16,724 --> 00:01:19,664 password to the users entered into HTML form. 35 00:01:19,664 --> 00:01:21,872 And this class is responsible for either 36 00:01:21,872 --> 00:01:23,624 presenting the user with the homepage if 37 00:01:23,624 --> 00:01:24,886 it's been logged in successfully. 38 00:01:24,886 --> 00:01:26,876 In other words, the password is what we expect and 39 00:01:26,876 --> 00:01:29,096 we've got a valid user, or if that isn't the 40 00:01:29,096 --> 00:01:31,652 case, it's going to reject the user and redirect him 41 00:01:31,652 --> 00:01:33,903 to go back to the login screen. So that's 42 00:01:33,903 --> 00:01:35,468 roughly what the LoginController is going to do. 43 00:01:35,468 --> 00:01:36,884 So let's write some code for that. 44 00:01:36,884 --> 00:01:39,870 [No Audio] 45 00:01:39,870 --> 00:01:41,616 Let's say that the controller returns back a 46 00:01:41,616 --> 00:01:42,828 string, which is either going to be a 47 00:01:42,828 --> 00:01:44,556 reference to the home page or some kind 48 00:01:44,556 --> 00:01:46,932 of instruction to redirect to the login page. 49 00:01:46,932 --> 00:01:47,988 Let's just keep it simple. 50 00:01:47,988 --> 00:01:50,004 We'll just return one of two strings, either 51 00:01:50,004 --> 00:01:52,224 slash home or slash login, depending on whether 52 00:01:52,224 --> 00:01:54,470 tha the user logged in successfully or didn't. 53 00:01:54,470 --> 00:01:57,087 Let's give it a service method, say. 54 00:01:57,087 --> 00:01:59,153 [No Audio] 55 00:01:59,153 --> 00:02:02,534 with a String username and a String password. 56 00:02:02,534 --> 00:02:04,633 Now obviously there are different types of framework 57 00:02:04,633 --> 00:02:07,117 out there to be able to code web apps in Java to the 58 00:02:07,117 --> 00:02:11,433 Spring MVC, for example, Java Servlets, JavaServer Faces 59 00:02:11,433 --> 00:02:13,716 we are going way back in time, Struts and those 60 00:02:13,716 --> 00:02:15,204 kinds of framework, and each one has 61 00:02:15,204 --> 00:02:16,896 its own different way of doing things. 62 00:02:16,896 --> 00:02:18,168 But don't worry about that now. 63 00:02:18,168 --> 00:02:19,538 Just focus on the fact that we're creating 64 00:02:19,538 --> 00:02:22,176 a LoginController which is hypothetical, and that 65 00:02:22,176 --> 00:02:24,276 LoginController sits on the web layer of 66 00:02:24,276 --> 00:02:26,534 the application and just purely deals with logic 67 00:02:26,534 --> 00:02:28,676 of working out where to send a user. 68 00:02:28,676 --> 00:02:30,346 In reality this method is just going 69 00:02:30,348 --> 00:02:32,376 to be dealing with http requests and 70 00:02:32,376 --> 00:02:34,080 http responses and that kind of thing. 71 00:02:34,080 --> 00:02:35,076 But we don't want to get bogged down 72 00:02:35,076 --> 00:02:36,540 with that because the principle is the same 73 00:02:36,540 --> 00:02:38,676 and indeed these two parameters using them and 74 00:02:38,676 --> 00:02:41,066 password could be annotated parameters which are bound 75 00:02:41,066 --> 00:02:43,274 to the controller upon servicing the web request. 76 00:02:43,274 --> 00:02:44,736 So don't worry about that for now. 77 00:02:44,736 --> 00:02:46,369 Just focus on the fact that the LoginController 78 00:02:46,369 --> 00:02:48,230 has one purpose and that one purpose. 79 00:02:48,230 --> 00:02:50,424 The thing it's supposed to know about, the thing it 80 00:02:50,424 --> 00:02:52,893 knows about that it's an expert and if you will, 81 00:02:52,893 --> 00:02:54,579 is to 82 00:02:54,579 --> 00:02:57,562 send the user to the home page 83 00:02:57,562 --> 00:03:01,681 or send the user to the login page. In 84 00:03:01,681 --> 00:03:04,104 other words, this class, this method specifically just 85 00:03:04,104 --> 00:03:06,444 knows about the home and the login pages and 86 00:03:06,444 --> 00:03:08,728 that's as it should do. Because the controller classes 87 00:03:08,728 --> 00:03:10,980 in the presentation there, so it doesn't know anything 88 00:03:10,980 --> 00:03:13,166 about how to actually authenticate a user and that's 89 00:03:13,166 --> 00:03:14,964 because to do so we need to actually start 90 00:03:14,964 --> 00:03:17,498 thinking about the layer underneath and that's the 91 00:03:17,498 --> 00:03:19,382 authentication service because the controller 92 00:03:19,382 --> 00:03:21,036 needs an authentication service to do 93 00:03:21,036 --> 00:03:22,476 its work as we'll see in the next video. 94 00:03:22,476 --> 00:03:24,218 So let's start flushing out the controller 95 00:03:24,218 --> 00:03:26,713 with the authentication service up next.