Questions Management – Core Category Service – Build API adding new category using Spring WebFlux and Spring Data MongoDB Reactive

Check out the full series of Questions Management tutorial here.

To build the API adding a new category in the Core Category Service, there are a few things we need to do first:

The first thing we need to add an interface named CategoryRepository to manipulate with MongoDB server.

This interface will be in the com.huongdanjava.categoryservice.repository package, which extends from the ReactiveMongoRepository interface of Spring Data MongoDB Reactive.

The content of the CategoryRepository interface is as follows:

with Category object:

You can see the tutorial Reactive REST APIs with Spring Data MongoDB Reactive and Spring WebFlux for more information.

Next we will configure the connection to the MongoDB server.

Currently, I am using MongoDB server without authentication mode, so I just declare the following in application.properties file:

I will run this service using port 8082 so I will add the server.port property in the application.properties file as follows:

OK, everything is ready, now I will go to the main part of this tutorial.



I will create a controller named CategoryController with the following content:

With this declaration, I expose APIs for the Core Category Service with the request URL starting with “/category”.

To work with the MongoDB server, we need to use the CategoryRepository interface, so I will declare it in the CategoryController class as follows:

To build the API adding a new category, I would add a new method to expose a POST request “/add” with data in the body as the Category object:

Because Spring Data MongoDB Reactive has provided us the save() method to save the data to MongoDB, so I just need to call for using this method:

At this point, we have completed the construction for the API adding a new category for the Core Category Service, let’s try to test it.

Request:

Questions Management - Core Category Service - Build API adding new category using Spring WebFlux and Spring Data MongoDB Reactive

Response:

Questions Management - Core Category Service - Build API adding new category using Spring WebFlux and Spring Data MongoDB Reactive


The last thing we need to do is: add new Unit Test to the code we just added.

The Category object and CategoryRepository, nothing to test, right? What we need to test is the CategoryController.

I will create a class named CategoryControllerTest located in the src/test/java package to test the CategoryController class.

The principle of the Unit Test is that we will mock all the code invoked outside the method we need to test. Currently, in our CategoryController class there is only one method, and in this method, it is calling the save() method of the CategoryRepository class, so we will mock the CategoryRepository class.

Check out more about Mockito’s @Mock annotation here.

Next, I will use @InjectMocks annotation to use the mock object of CategoryRepository in CategoryController as follows:

To use mock objects, we have to initialize them before testing a method, so I will add a method with the JUnit @Before annotation as follows:

The code to test the createCategory() method is as follows:

Running “Maven test” in the STS, you will not see any problem.

Add Comment