Questions Management – Core Question Service – Build the API to get all the questions using Spring WebFlux and Spring Data MongoDB Reactive

Check out the full series of Questions Management tutorial here.

In the previous tutorial, we prepared all the necessary configurations to be able to build the API to retrieve all the questions contained in the MongoDB database: a Question document object containing the information of a question, a QuestionRepository for manipulation with MongoDB, a QuestionController that defines the Core Question Service APIs will start with “/question” and the connection information to the MongoDB server is configured in the application.properties file. Now, we are going to build this API.

To build an API that takes all the questions, I will add a method to expose a GET request “/all”:

Since Spring Data Mongo Reactive has provided us with a method to search all the questions, our job is just to use it:

That’s it! 🙂 Let’s test us.

Suppose, my database has the following questions:

Questions Management – Core Question Service – Build the API to get all the questions using Spring WebFlux and Spring Data MongoDB Reactive

then when I run the request to get all the questions, the result will be:

Questions Management – Core Question Service – Build the API to get all the questions using Spring WebFlux and Spring Data MongoDB Reactive


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

In my last post, I created a new QuestionController class called QuestionControllerTest, mock object for QuestionRepository, and inject this Mock object into the QuestionController class, initializing Mock objects each time running a test case:

Now, I will add a method to test for the method of the findAllQuestions() of QuestionController:

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

Add Comment