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:
1 |
Mono<HttpStatus> deleteCategory(String categoryId); |
with the implementation of this method in the CoreCategoryServiceImpl class is as follows:
1 2 3 4 5 6 7 8 9 10 11 |
@Override public Mono<HttpStatus> deleteCategory(String categoryId) { WebClient webClient = WebClient.builder() .baseUrl(getServiceUrl()) .build(); return webClient.delete() .uri("/category/" + categoryId) .exchange() .map(response -> response.statusCode()); } |
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:
1 2 3 4 5 |
@DeleteMapping("/{id}") public Mono<ResponseEntity<Void>> deleteCategory(@PathVariable(value = "id") String categoryId) { return coreCategoryService.deleteCategory(categoryId) .map(statusCode -> new ResponseEntity<Void>(statusCode)); } |
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:
the when I request to remove the category with id “5b469aafe77a570af0f3cf33”, the result will be:
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.huongdanjava.categoryservice; import org.junit.Before; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import com.huongdanjava.categoryservice.service.CoreCategoryService; public class CategoryControllerTest { @Mock private CoreCategoryService coreCategoryService; @InjectMocks private CategoryController categoryController; @Before public void init() { MockitoAnnotations.initMocks(this); } } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@Test public void testDeleteExistingCategory() { // Mock deleteCategory() method of CoreCategoryService to return status code 200 when(coreCategoryService.deleteCategory("123")).thenReturn(Mono.just(HttpStatus.OK)); // Call method Mono<ResponseEntity<Void>> delete = categoryController.deleteCategory("123"); ResponseEntity<Void> responseEntity = delete.block(); // Assertions assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); } |
A method to test for the absence of category based on passing id:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@Test public void testDeleteNoExistingCategory() { // Mock deleteCategory() method of CoreCategoryService to return status code 404 when(coreCategoryService.deleteCategory("123")).thenReturn(Mono.just(HttpStatus.NOT_FOUND)); // Call method Mono<ResponseEntity<Void>> delete = categoryController.deleteCategory("123"); ResponseEntity<Void> responseEntity = delete.block(); // Assertions assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode()); } |
Run “Maven test” in STS or “mvn test” with Apache Maven, you will not see any errors.