0

i want to match output of get-request, and match with manual provided json string, basically copy one json string coming out of whole output and test. Tried lot of ways but its not happening, i can't delete all rows from database to match one string to one, so, i want to match only one json string -- Like this below

{
        "instanceName": "",
        "read": 1,
        "swProduct": "Area 1",
        "swProductModule": "Regular 1"
    },

Here's, my test code ---


@SpringBootTest(classes = TestApplication.class)
@ActiveProfiles("dev")
@AutoConfigureMockMvc

public class SwStatusCheckTest {

    @Autowired
    private MockMvc mvc;

    @IfProfileValue(name = "spring.profiles.active", values = { "dev" })
    @Test
    @DisplayName("GET Method /SwCheckStatus check success")
    public void testStatusSuccess() throws Exception {

        MvcResult result =  mvc.perform(get("/status/SwCheckStatus"))
                .andDo(print())
                .andExpect(status().isOk())
                .andReturn();
                //.andExpect(content().json("{\"services\":[\"OutboundMessageService\"]}", true));

        String actualJson = result.getResponse().getContentAsString();
        String expectedJson = "[{\"instanceName\":\"Instance C\", \"read\" : 1, \"swProduct\" : \"Area 3\", \"swProductModule\" : \"Spring Boot 3\"}]";


        assertThat(expectedJson).isIn(actualJson);

}
}

Output of whole Postman json result looks like this ---

[
    {
        "instanceName": "",
        "read": 1,
        "swProduct": "Area 1",
        "swProductModule": "Regular 1"
    },
    {
        "instanceName": "",
        "read": 1,
        "swProduct": "Area 1",
        "swProductModule": "Regular 2"
    },
    {
        "instanceName": "",
        "read": 1,
        "swProduct": "Area 1",
        "swProductModule": "Spring Boot 1"
    },
    {
        "instanceName": "",
        "read": 1,
        "swProduct": "Area 1",
        "swProductModule": "Spring Boot 2"
    },

Intellij says -- its expecting and String matches... but test failed -- check attachment --- [enter image description here][1]

Any help of insights to perform the test is greatly appreciated. [1]: https://i.sstatic.net/FE2dT.jpg

2 Answers 2

1

isIn is not for comparing strings, but arrays.

You can compare JSON as Map, with jackson-databind and Google Guava:


import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.IfProfileValue;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

@SpringBootTest(classes = TestApplication.class)
@ActiveProfiles("dev")
@AutoConfigureMockMvc

public class SwStatusCheckTest {

    @Autowired
    private MockMvc mvc;

    @IfProfileValue(name = "spring.profiles.active", values = { "dev" })
    @Test
    @DisplayName("GET Method /SwCheckStatus check success")
    public void testStatusSuccess() throws Exception {

        MvcResult result = mvc.perform(get("/status/SwCheckStatus"))
            .andDo(print())
            .andExpect(status().isOk())
            .andReturn();
        //.andExpect(content().json("{\"services\":[\"OutboundMessageService\"]}", true));

        ObjectMapper mapper = new ObjectMapper();

        List<? extends Map<String, Object>> actualJson = mapper.readValue(result.getResponse().getContentAsString(),
            List.class);

        Map<String, Object> expected = Map.of("instanceName", "", "read", 1, "swProduct", "Area 1", "swProductModule",
            "Regular 1");

        boolean contains = false;
        for (Map<String, Object> a : actualJson) {
            boolean res = Maps.difference(a, expected).areEqual();
            if (res) {
                contains = true;
                break;
            }
        }
        assertThat(contains).isTrue();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

If I'm reading your question right as well as the image you posted, the problem is either the expected/actual Strings or the condition (assertion) defined in the test. The "actual" String value is an array of several elements, the "expected" is an array of only one element. Since the array of just one element is NOT contained in the array of several elements, it won't work.

If the code is changed such that the "expected" value you're looking for is written as JUST the object (without its enclosing array)

{ ... your object here ... }

Then the expected "isIn" actual assertion should work.

1 Comment

i need to match only one string... coming from output of result.getResponse().getContentAsString();

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.