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:
1 |
Mono<Category> updateCategory(String categoryId, Category category); |
with the implementation of this method in the CoreCategoryServiceImpl class is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 |
@Override public Mono<Category> updateCategory(String categoryId, Category category) { WebClient webClient = WebClient.builder() .baseUrl(getServiceUrl()) .build(); return webClient.put() .uri("/category/" + categoryId) .body(BodyInserters.fromObject(category)) .retrieve() .bodyToMono(Category.class); } |
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:
1 2 3 4 5 6 |
@PutMapping("/{id}") public Mono<ResponseEntity<Category>> updateCategory(@PathVariable(value = "id") String categoryId, @RequestBody Category category) { return coreCategoryService.updateCategory(categoryId, category) .map(c -> ResponseEntity.ok(c)) .defaultIfEmpty(ResponseEntity.notFound().build()); } |
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:
If I update the category with the id “5b234312f2581f2c0c276a58” then the results will be as follows:
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:
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 for the method of updateCategory() 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 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
@Test public void testUpdateExistingCategory() { // Category information need to be updated Category updateCategory = new Category(); updateCategory.setCode("ABC"); updateCategory.setName("ZXCV"); // Category object will be returned after updating Category category = new Category(); category.setId("123"); category.setCode("ABC"); category.setName("ZXCV"); // Mock updateCategory() method of CoreCategoryService to return above category when(coreCategoryService.updateCategory("123", updateCategory)).thenReturn(Mono.just(category)); // Call method Mono<ResponseEntity<Category>> update = categoryController.updateCategory("123", updateCategory); ResponseEntity<Category> responseEntity = update.block(); // Assertions assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); Category responseCategory = responseEntity.getBody(); assertEquals("ZXCV", responseCategory.getName()); } |
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 14 15 16 17 18 |
@Test public void testUpdateNoExistingCategory() { // Category information need to be updated Category updateCategory = new Category(); updateCategory.setCode("ABC"); updateCategory.setName("ZXCV"); // Mock updateCategory() method of CoreCategoryService to return an empty Mono when(coreCategoryService.updateCategory("123", updateCategory)).thenReturn(Mono.empty()); // Call method Mono<ResponseEntity<Category>> update = categoryController.updateCategory("123", updateCategory); ResponseEntity<Category> responseEntity = update.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.