AssertJ JSON property check
Asked Answered
B

5

7

I have JSONObject instance which contains some property,

{
"name":"testName",
"age":"23"
}

i use the following assert, but it fails. Is this correct approach to test JSON in assertj.

assertThat(jsonObject).hasFieldOrProperty("name");
Berenice answered 21/1, 2020 at 10:29 Comment(4)
I'm not sure which JSONObject class you are using. It would be better to provide the full class name including package so that we can help you more precisely. In any case you should consider using JsonUnit to do assertj assertions on json content: github.com/lukas-krecan/JsonUnitSpeller
Apart from what @Speller has mentioned, can you also mention which library this assertThat() method is in? Can't see this in JUnit 3.8.1.Sporangium
My JSONObject class from org.json.JSONObjectBerenice
@SreeKumar i use org.assertj.core.api.Assertions APIBerenice
S
3

I think it has to do with the fact the JSONObject is like a map which has key-value pairs, while AssertJ expects Java bean-style objects to check if a property exists. I understood this from the document at https://joel-costigliola.github.io/assertj/core/api/org/assertj/core/api/AbstractObjectAssert.html#hasFieldOrProperty(java.lang.String). Hope I am looking at the right place.

I mean to say that a map or JSONObject doesn't have fields declared in it for AssertJ to look for.

You may use JSONObject.has( String key ) instead, I think.

Sporangium answered 21/1, 2020 at 10:52 Comment(0)
B
9

If you want to do any serious assertions on JSON object, I would recommend JsonUnit https://github.com/lukas-krecan/JsonUnit

Botts answered 21/1, 2020 at 22:35 Comment(0)
T
4

If you use SpringBoot you can use the custom impl. for Assertj

    private final BasicJsonTester json = new BasicJsonTester(getClass());

    @Test
    void testIfHasPropertyName() {
        final JSONObject jsonObject = new JSONObject("{\n" +
                "\"name\":\"testName\",\n" +
                "\"age\":\"23\"\n" +
                "}");
        
        assertThat(json.from(jsonObject.toString())).hasJsonPath("$.name");
    }

Truditrudie answered 20/9, 2022 at 2:8 Comment(0)
S
3

I think it has to do with the fact the JSONObject is like a map which has key-value pairs, while AssertJ expects Java bean-style objects to check if a property exists. I understood this from the document at https://joel-costigliola.github.io/assertj/core/api/org/assertj/core/api/AbstractObjectAssert.html#hasFieldOrProperty(java.lang.String). Hope I am looking at the right place.

I mean to say that a map or JSONObject doesn't have fields declared in it for AssertJ to look for.

You may use JSONObject.has( String key ) instead, I think.

Sporangium answered 21/1, 2020 at 10:52 Comment(0)
P
0

Fist, you need to traverse the keysets (nodes) using the map class, then verify if the keyset contains the particular node you are looking for.

Map<String, Object> read = JsonPath.read(JSONObject, "$");
assertThat(read.keySet()).contains("name");
Product answered 25/7, 2020 at 3:13 Comment(0)
E
0

Spring Boot has a dependency on JSONAssert that you can use:

import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;

var actual = ...
var expected = """
    {
    "property": "value"
    }
    """
assertEquals(expected, actual, false);
Eldenelder answered 15/2 at 10:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.