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

Check out the full series of Questions Management tutorial here.

In the previous tutorial, we have prepared all the necessary configurations to build API remove a category using Spring WebFlux in our Questions Management application such as: a Category object that holds information about a category, a CoreCategoryService interface with a CoreCategoryServiceImpl implementation that is responsible for working with the Core Category Service, a CategoryController defines the APIs of the API Category Service that will start with “/category” and information about the Core Category Service is configured in the application.properties file. Now, let’s build this API!

To build the category deletion API, I first add a new deleteCategory() 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 remove a category of this service with the “/category/{id}” URI.

Next, I will add a method call to the CoreCategoryService deleteCategory() method to expose a DELETE request “/category/{id}” in CategoryController as follows:

Now that we have completed the construction of the API remove a category in API Category Service, let’s test it.

Suppose our current Question Management application has the following categories:

Questions Management - API Category Service - Build API remove category using Spring WebFlux

the when I request to remove the category with id “5b469aafe77a570af0f3cf33”, the result will be:

Questions Management - API Category Service - Build API remove 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 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 the method of deleteCategory() 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.

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

Add Comment