Questions Management – API Category Service – Build API updating category using Spring WebFlux

Check out the full series of Questions Management tutorial here.

In the previous tutorial, we prepared all the necessary configurations to build an API to update a category: a Category object that holds information about a category, a CoreCategoryService interface with a CoreCategoryServiceImpl implementation to work with the Core Category Service, a CategoryController that defines the APIs of the API Category Service will start with “/category” and the Core Category Service information is configured in the application.properties file. Now, let’s build this API!

To build the API update category, first, I will add an updateCategory() method in the CoreCategoryService interface:

with the implementation of this method in the CoreCategoryServiceImpl class is as follows:

As you can see, here I have used the WebClient object to connect to the Core Category Service and call the API to update the category of this service with the “/category / {id}” URI.

Next, I’m going to add a method call to the CoreCategoryService’s updateCategory() method to expose a PUT request “/category / {id}” in CategoryController:

Now that we have completed the API updating category for API Category Service, let’s test it.

Suppose, our current Questions Management application has the following categories:

Questions Management – API Category Service – Build API updating category using Spring WebFlux

If I update the category with the id “5b234312f2581f2c0c276a58” then the results will be as follows:

Questions Management – API Category Service – Build API updating category using Spring WebFlux


Now we will add a new Unit Test for the code that we just added.

In the previous tutorial, I created a new class test for CategoryController named CategoryControllerTest, a Mock object for CoreCategoryService, and injected this Mock object into the CategoryController class, initializing the mock every time I run a test case:

Now we will add two methods to test for the method of updateCategory() of the CategoryController class as follows:

A method to test if there is a category based on passing id:

A method to test for the absence of category based on passing id:

Run “Maven test” in STS or “mvn test” with Apache Maven, you will not see any errors.

Đánh giá bài viết
Chia sẽ bài viết này ...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment