In the previous tutorial, we prepared all the necessary configurations to build API retrieving all categories from the Core Category Service in API Category Service as: a Category object to contain information about a category, a CoreCategoryService interface, with implementation is the CoreCategoryServiceImpl, is responsible for handling the Core Category Service, a CategoryController that defines the APIs for API Category Service that will start with “/category” and information about the Core Category Service configured in the application.properties file. Now, let’s build this API!
To build the API retrieving all the categories, I would first add a findAll() method in the CoreCategoryService interface:
1 |
Flux<Category> findAll(); |
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 Flux<Category> findAll() { WebClient webClient = WebClient.builder() .baseUrl(getServiceUrl()) .build(); return webClient.get() .uri("/category/all") .retrieve() .bodyToFlux(Category.class); } |
As you can see, here I have used the WebClient object to connect to the Core Category Service and call the API retrieving all categories of this service with the “/category/all” URI.
Next, I’m going to add a method to expose a GET request “/all” in CategoryController:
1 2 3 4 |
@GetMapping("/all") public Flux<Category> findAllCategories() { } |
Our task is to just call the CoreCategoryService’s findAll() method:
1 2 3 4 |
@GetMapping("/all") public Flux<Category> findAllCategories() { return coreCategoryService.findAll(); } |
Now that we have finished building the API retrieving all the categories for the API Service Category, let’s test it.
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 I’m going to add a method to test the findAllCategories() method of the CategoryController class, like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
@Test public void testFindAllCategories() { // Create category document to return when using findAll Category category = new Category(); category.setId("123"); category.setCode("ABC"); category.setName("ZXC"); // Mock findAll() method of CoreCategoryService to return a Flux when(coreCategoryService.findAll()).thenReturn(Flux.just(category)); // Call method Flux<Category> categories = categoryController.findAllCategories(); Category result = categories.blockFirst(); // Assertions assertEquals("123", result.getId()); assertEquals("ABC", result.getCode()); assertEquals("ZXC", result.getName()); } |
Run “Maven test” in STS or “mvn test” with Apache Maven, you will not see any errors.