From the course: Test-Driven Development in Spring Boot with JUnit and Mockito

Unlock this course with a free trial

Join today to access over 24,900 courses taught by industry experts.

Implementing the method in the service layer

Implementing the method in the service layer

- [Instructor] Now let's implement a method in the surface layer. We're going to write just enough code in our task surface to make this test pass, and it's absolutely okay to leave room for optimization. We'll fix that in the refactor phase. So first of all, I'm going to annotate my task service with @ service. So it's recognized by Spring as a service beam. So my task service is actually relying on the task repository. I'm going to make sure that it's injected with constructor injection. Okay, time to have a look at this method. So for now, it's returning null and I don't want that. So let's start by using the repository to find the task in a data layer that I do want to update. I'm going to say taskRepository.findById, and use the ID that was passed in as an argument. Then I'm going to say, Hey, if that optional task is present, then please go ahead and take it out of the optional, set the status to the passed in status, and then use the task repository to save it. And I'm actually…

Contents