I have an api that returns the price for a product with two decimals and that should happen even when these decimals are zero, i.e, 100.00. However, the mockito tests are failing and stripping one of these zeros and I'm not sure why. I have tried to force the scale to have two zeros but that didn't work either, even though the api itself works as expected.
@Test
public void testGetAllProductsOneItemOnlySo() throws Exception {
UUID productId = UUID.fromString("ac358df7-4a38-4ad0-b070-59adcd57dde0");
ProductQueryDto productQueryDto = new ProductQueryDto(productId, "product", "prod description", new BigDecimal("100.00").setScale(2, RoundingMode.HALF_UP), null, null);
List<ProductQueryDto> productQueryDtoList = List.of(productQueryDto);
when(productQueryService.getAllProducts()).thenReturn(productQueryDtoList);
RequestBuilder request = MockMvcRequestBuilders
.get("/api/adverts/product")
.accept(MediaType.APPLICATION_JSON);
mockMvc.perform(request).andReturn();
HashMap<String, Object> result = new HashMap<>();
result.put("products", productQueryDtoList);
String json = asJsonString(result);
mockMvc.perform(request)
.andExpect(status().is2xxSuccessful())
.andExpect(content().json(json, true))
.andExpect(jsonPath("$.products[0].price").value(new BigDecimal("100.00").setScale(2, RoundingMode.HALF_UP)))
.andReturn();
}
Thank you.