Hey there, are you aware that GitHub Copilot is a
fantastic sidekick for Visual Studio Code? It provides Java code
suggestions and aiding with programming tasks. I'm Rory Preddy, a
Principal Cloud advocate at Microsoft, and in this video we're
going to explore a practical example of using Copilot with
Java and Spring Boot, and we're going to go in
and refactor an existing project.
If you're new to Copilot, consider watching a intro video
on this powerful AI tool before joining me here. So
first let's look at our project. Now I've got my
pom dot XML and my pom is my project configuration,
and in there you'll see there I've got the actuator,
jpa Web.
My H2 database and the starter test and this allows
me to use all of the built in Spring boot
features that I'm going to highlight. And then in my
application I've got a list of to do's and I
take those list of to do's and I can save
them and then I find them all. So let's run
this now and let's get our list of to do's
and what you'll notice here is that I'm only actually
calling.
A database and retrieving the list of todos. I'm calling
the Todo repository which extends the JPI repository and then
I'm getting a list of todos. What I want to
do is actually have a REST controller. So I'm going
to go ahead and create a REST controller. So I'm
going to go new file and I'm going to go
todo controller dot Java.
And in that todocontroller dot Java I'm going to make
sure that that Todocontroller utilizes some of the Spring libraries.
So I'm going to go in and copy some of
the Spring libraries I want to use.
And then I'm going to go into my todo controller
and I'm going to start annotating it. And I want
to help Copilot understand what to do there. Generate a
REST controller with the path of/ Let's see what it
gives me here, and let's see it knows that I
need REST controller. That's great. Let's see if request mapping/
which is great. And then I want to actually add
validated.
Which will it allow me to validate that the class
is correct and then after that I can start actually
going in and creating my methods and variables. I want
to go in and 1st create a private variable of
final repository. You can see there it's actually warning me
that it's not actually being used right now. Wow, it
actually says, oh, you want to use this? Yeah, please.
Thanks. That's great.
Next, I want to actually go in and start creating
my POST method. Let's go generate a POST method to
create it to do. OK, that's great, it's going to
go post mapping and let's see what the method is
that it's once there. OK, response entity create. I'm going
to accept that. I want to change it a little
bit there because the industry standard.
Is to actually go in and create a Uri or
you can say there we go. I didn't see that
but it Uri create and that's great, it's it's actually
went and created that. Next I want to go in
and create a GET method. So generate a GET method
to go in and retrieve the to Do's, generate a
message list to list all the to do's. That's great
get mapping I like that and then find all.
It didn't really understand what I was saying there, so
I want to actually help it out there and I'm
going to go public response, Response entity. Yeah, I like
what it's saying there, but I want to actually just
change that also to say List to Dos Oh, I
need to go in and create my generics.
Here we go. And then finally I'm going to go
return response entity todos O. Those are my 2 methods
that I've got here. I've got my get maing and
I did it through the comments.
And I created my front of my rest controller request
mapping and validation. Let's test this quickly and then we
can see if this runs and then we're going to
add a unit test for our project. I'm going to
go into start, I'm going to hit run and with
the Spring Boot actuator it's going to go in and
create my POST mappings and then also my git mapping.
You can see that even with the actuator it shows
me.
Where I've added the URLs there and then I can
go in there and I can click on the URL.
Then you can see there I've got my objects retrieved
already. Quite quite simple and quick to do and let's
go in and now create our unit test. So let's
go into our demo class here and when you go
new create a todo controller test dot Java.
And I want to help Copilot understand what to do
there by actually telling it which classes I want to
use. With my imports you can actually use the intellisense,
but I I find it that if I know which
classes I'm going to use, it's better to actually say
hey, I want to use mock MVC instead of Mockito.
So next I want to go in on my Todo
controller class and I'm whoa, I want to actually just
add in one more import here.
For Jackson, because I want to go in and Marshall
those classes. So I'm going to annotate my class with
at Webmvctest, which is a great class to actually use
for MVC, and then I'm going to also autoconfigure it.
And what this does is that it actually goes in
and it sets up everything for my class.
To be able to use mocking with mock MVC. There
we go, autoconfigure mock MVC. Great. Now let's actually go
in and wire up our beans here. First I want
to go order wire create my mock bean here. Let's
go in and see if Copilot can help us knew
exactly what I wanted there and mock MVC. Next I
want to go mock bean and I'm going to mock
my repository with Copilot's assistance.
And let's see if it knows what I'm talking about.
Yeah, very, very clever, Copilot. Next, I want to go
in and create my my unit test. Now let's see
if it actually is clever enough to go create my
unit test. Else, and I'm going to go look at
that generates a test method for the post method. I
want to see what that is there and I can
actually go in and cycle through it if I don't
like it. Let's go test and see what it does
there, there we go. And let's see if I can
find out something to go view.
Completion panel and I can open my completion panel there
and I can go back in there and there. In
case I can go see suggestion one, suggestion two, Let's
see which one I like here. Set description completed. I
like this one and let's try that one. I'm going
to go accept the method here.
And over here it's going to go Description completed. Wow
learn J unit 5. That's brilliant. So now I can
go in there and see if we can run it.
Else I'm going to go in and refactor my test
to see if I can get the entire unit and
it runs it. It loves it. So now I can
go in and run the entire project start to finish.
I want to actually just open up my terminal window
here. So I'm going to go terminal new terminal here
and I can go mvn test.
And package and it should run start to finish. With
that and I can do a final Test. At the
end of this I can actually go in and test
the URLs again, but I'm quite happy that everything is
running. Copilot allowed me to choose the method that I
wanted and also reiterate and also generate from the tests.
GitHub Copilot is an incredible tool for streamlining.
Work and increasing efficiency, allowing us to focus on complex
and creative tasks. However, remember that Copilot isn't perfect and
as developers we're responsible for our own code, whether AI
assisted or not. Thanks for watching if you enjoyed the
video, please like and subscribe, explore other videos and learn
how you can use Copilot with various languages and frameworks.