0

My test with Junit5 doesn't pass.

JSON path "$.balance" Expected: is <1000> but: was <1000>

I used the tree following modes. First one is the method that returns the initial error.

Other two Java complains with "cannot cast from BigDecimal to Integer".

Where is my wrong?

BigDecimal balance = new BigDecimal(1000);
        
when(bankAccountTransactionsService.balance()).thenReturn(balance);
        
mvc.perform(get("/api/v1/balance")).andExpect(status().isOk())
                .andExpect(jsonPath("$.balance", Matchers.comparesEqualTo(bankAccountTransactionsService.balance())));
mvc.perform(get("/api/v1/balance")).andExpect(status().isOk())
                .andExpect(jsonPath("$.balance", Matchers.comparesEqualTo(balance)));
mvc.perform(get("/api/v1/balance")).andExpect(status().isOk())
                .andExpect(jsonPath("$.balance", is(balance)));
sineverba
  • 5,059
  • 7
  • 39
  • 84
  • related: [BigDecimal equals() versus compareTo()](https://stackoverflow.com/questions/6787142/bigdecimal-equals-versus-compareto) – jhamon Feb 24 '22 at 14:52

1 Answers1

1

Probably because the types do not match. You can fix it with balance.intVal():

BigDecimal balance = new BigDecimal(1000);
when(bankAccountTransactionsService.balance()).thenReturn(balance);
mvc.perform(get("/api/v1/balance")).andExpect(status().isOk())
                .andExpect(jsonPath("$.balance", is(balance.intVal())));
MevlütÖzdemir
  • 3,180
  • 1
  • 23
  • 28