0

I have a test method that is provided,

@Test
    public void calculateReward() throws Exception {

        when(userService.findById(any(Long.class))).thenReturn(Optional.of(user));
        int steps = 1000;

        user.setCurrentSteps(steps);
        user.setTotalSteps(steps);

        when(userService.save(any(User.class))).thenReturn(user);
        Map<String, Double> map = new HashMap<>();
        map.put("EUR", 1.0);
        when(currencyUtilities.getCurrencyMap()).thenReturn(map);

        mockMvc.perform(put("/api/v1/users/calculateReward")
                .param("userId", String.valueOf(user.getId())))
                .andExpect(
                        status().isCreated()
                ).andExpect(
                content().contentType(MediaType.APPLICATION_JSON_UTF8)
        ).andDo(print())
                .andExpect(
                        jsonPath("$.name", is(user.getName()))
                ).andExpect(
                jsonPath("$.currency", is(user.getCurrencyName()))
        ).andExpect(
                jsonPath("$.reward", is(1.0)));
    }

I get the error message,

java.lang.AssertionError: JSON path "$.reward"
Expected: is <1.0>
     but: was "1.00"
Expected :is <1.0>
Actual   :"1.00"

What is the issue here?

1 Answer 1

1

Just as the error message says: the test expects to see the number 1.0 in the JSON it receives (is(1.0)), but the JSON actually contained the string "1.00" at that path. Read https://github.com/json-path/JsonPath for the meaning of paths, but $.reward is just the "reward" field of the root object. So it should look like

{
  "reward": 1.0,
  ... other fields including "name" and "currency"
}

but was

{
  "reward": "1.00",
  ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

How do I correct it? Sorry if its dumb, but I am not able to. Changing ti to the jsonPath("$.reward", is(1.00))); doesnt solve my problem.
is("1.00"). But first you need to figure out whether it's the test or the system you are testing which should be corrected! Especially since you say the test is "provided".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.