2

I run a unit test where I would like to get a JSON object and need to compare it with values. The end-point is provided below,

private static final String TOTAL_REWARD = "total_reward_";
private static final String EUR = "EUR";

@GetMapping(value = "/findUsersPayouts")
    public ResponseEntity<String> findUsersPayoutList(@RequestParam("userId") Long userId) {

        Optional<User> optional = userService.findById(userId);

        if (!optional.isPresent()) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        }

        User user = optional.get();

        List<Reward> rewards = user.getRewards();


        JSONObject obj = new JSONObject();

        obj.put("id", user.getId());
        obj.put("name", user.getName());

        JSONArray array = new JSONArray();

        for (Reward reward : rewards) {

            JSONObject currData = new JSONObject();

            double amount = reward.getAmount();
            String currencyName = user.getCurrencyName();

            Map<String, Double> currencyMap = currencyUtilities.getCurrencyMap();

            currData.put(TOTAL_REWARD + EUR, amount);


            /*
             * convert the reward amount to 2 decimals limit
             * */
            DecimalFormat df = new DecimalFormat("#.00");

            double rewardInLocalCurrency = amount * currencyMap.get(currencyName);
            String rewd = df.format(rewardInLocalCurrency);

            currData.put(TOTAL_REWARD + currencyName, rewd);
            array.put(currData);
        }

        obj.put("rewards", array);
        return ResponseEntity.status(HttpStatus.CREATED).contentType(MediaType.APPLICATION_JSON_UTF8).body(obj.toString());
    }

I have the test here,

@Test
    public void findUsersPayoutList() throws Exception {

        when(userService.findById(any(Long.class))).thenReturn(Optional.of(user));

        Reward reward = new Reward();
        reward.setUser(user);
        reward.setId(55L);
        reward.setAmount(1);

        Reward reward1 = new Reward();
        reward1.setUser(user);
        reward1.setId(57L);
        reward1.setAmount(1);

        user.addReward(reward);
        user.addReward(reward1);

        Map<String, Double> map = new HashMap<>();
        map.put("EUR", 1.0);

        when(currencyUtilities.getCurrencyMap()).thenReturn(map);
        JSONArray rewards = new JSONArray();
        JSONObject object = new JSONObject();
        object.put("total_reward_EUR :", 1);

        rewards.put(object);
        rewards.put(object);

        mockMvc.perform(get("/api/v1/users/findUsersPayouts").param("userId", String.valueOf(user.getId())))
                .andExpect(
                        status().isCreated()
                ).andExpect(
                content().contentType(MediaType.APPLICATION_JSON_UTF8)
        ).andDo(print())
                .andExpect(
                        jsonPath("$.id", is(user.getId().intValue()))
                ).andExpect(
                jsonPath("$.name", is(user.getName()))
        ).andExpect(
                jsonPath("$.rewards[0].['total_reward_EUR :']", is("1.00"))
        ).andExpect(
                jsonPath("$.rewards[1].['total_reward_EUR :']", is("1.00"))
        );
    }

When I run the test, I get the output,

java.lang.AssertionError: No value at JSON path "$.rewards[0].['total_reward_EUR :']"

I debug and acquite the value provided,

enter image description here

What is wrong here with the test?

0

1 Answer 1

1

You should use it like $.rewards[0].['total_reward_EUR'] to access the total_reward_EUR property of first object at rewards array in your json.

Sign up to request clarification or add additional context in comments.

Comments

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.