[No Audio]
In this video we're going to start to think
about how we can code up a layered architecture.
So in other words, an architecture with
respects the three layers you have in
a web application, that's presentation layer, service
layer, and a data or persistence layer.
And what we're going to do with this, is
it will enable us to describe basically how
we approach testing with mock objects, how we
apply that to this layered architecture.
So the next few videos might not seem so relevant
to unit testing, but they are because it's important to
understand how the lead architecture is coded, because
then we can explain how we apply Mockito to that.
So just hang in there for these next few videos.
Bear with us while we cover this up,
and then we'll pick up the Mockito testing
aspect of it in a few videos time.
So let's jump in now, and look at the controller.
So we've got our project set up now,
we can use Mockito and we've got JUnit installed.
Let's just remove this code here because
we don't really need this. That was just a test,
we had everything set up okay.
that's gone. Now it's plugging
a couple of classes which interact
with each other that we can demonstrate Mockito with.
So if I go into here in the production source
folder here and we'll just create a LoginController.
So let's suppose this LoginController is responsible
for serving web requests in a web application.
So Http request comes in which passes the username and
password to the users entered into HTML form.
And this class is responsible for either
presenting the user with the homepage if
it's been logged in successfully.
In other words, the password is what we expect and
we've got a valid user, or if that isn't the
case, it's going to reject the user and redirect him
to go back to the login screen. So that's
roughly what the LoginController is going to do.
So let's write some code for that.
[No Audio]
Let's say that the controller returns back a
string, which is either going to be a
reference to the home page or some kind
of instruction to redirect to the login page.
Let's just keep it simple.
We'll just return one of two strings, either
slash home or slash login, depending on whether
tha the user logged in successfully or didn't.
Let's give it a service method, say.
[No Audio]
with a String username and a String password.
Now obviously there are different types of framework
out there to be able to code web apps in Java to the
Spring MVC, for example, Java Servlets, JavaServer Faces
we are going way back in time, Struts and those
kinds of framework, and each one has
its own different way of doing things.
But don't worry about that now.
Just focus on the fact that we're creating
a LoginController which is hypothetical, and that
LoginController sits on the web layer of
the application and just purely deals with logic
of working out where to send a user.
In reality this method is just going
to be dealing with http requests and
http responses and that kind of thing.
But we don't want to get bogged down
with that because the principle is the same
and indeed these two parameters using them and
password could be annotated parameters which are bound
to the controller upon servicing the web request.
So don't worry about that for now.
Just focus on the fact that the LoginController
has one purpose and that one purpose.
The thing it's supposed to know about, the thing it
knows about that it's an expert and if you will,
is to
send the user to the home page
or send the user to the login page. In
other words, this class, this method specifically just
knows about the home and the login pages and
that's as it should do. Because the controller classes
in the presentation there, so it doesn't know anything
about how to actually authenticate a user and that's
because to do so we need to actually start
thinking about the layer underneath and that's the
authentication service because the controller
needs an authentication service to do
its work as we'll see in the next video.
So let's start flushing out the controller
with the authentication service up next.