Unit Test JSON Objects using JSONAssert

Java featured image

1. Overview

Nowadays no one can deny the importance of unit testing in software development, but you might notice how is so complicated to test JSON objects using org.assertj.core.api.Assertions only.

Fortunately and thanks to JSONAssert you can write JSON unit tests with less code and less complexity.

In this article, I will show you how to unit test JSON Objects using JSONAssert.

2. Implementation

1. First of all, add JSONAssert dependency to your project’s pom.xml.

<dependency>
    <groupId>org.skyscreamer</groupId>
    <artifactId>jsonassert</artifactId>
    <version>1.5.0</version>
    <scope>test</scope>
</dependency>

2. The JSONAssert syntax is as follow.

JSONAssert.assertEquals(message, expected, actual, compareMode);
  • message : displayed in case of assertion failure.
  • expected : expected JSON object as a String.
  • actual : JSON Object to compare, it can be JSONObject, JSONArray or String.
  • compareMode : it defines the behavior of comparing the JSON objects :
    • STRICT : not extensible JSON object, and strict array ordering.
    • LENIENT : extensible JSON object, and non-strict array ordering.
    • NON_EXTENSIBLE : not extensible JSON object, and non-strict array ordering.
    • STRICT_ORDER : extensible JSON object, and strict array ordering.

The extensibility means tolerates new fields and new elements to be added to the JSON object.

3. Examples of unit tests using JSONAssert.

public class AssertJsonExampleTests {

    @Test
    public void should_be_equals_when_is_same_objects_and_strict_mode_test() throws JSONException {

        final String expected =
                "{" +
                        "'id' : '123456'," +
                        "'name' : 'Toto'" +
                "}";
        final  String actual =
                "{" +
                        "'id' : '123456'," +
                        "'name' : 'Toto'" +
                "}";
        JSONAssert.assertEquals(expected, actual, JSONCompareMode.STRICT);
    }

    @Test
    public void should_not_be_equals_when_is_not_same_objects_and_strict_mode_test() throws JSONException {

        final String expected =
                "{" +
                        "'id' : '123456'," +
                        "'name' : 'Toto'" +
                "}";
        final  String actual =
                "{" +
                        "'id' : '456789'," +
                        "'name' : 'Tata'" +
                "}";
        JSONAssert.assertNotEquals(expected, actual, JSONCompareMode.STRICT);
    }

    @Test
    public void should_be_equals_when_is_different_fields_order_and_strict_mode_test() throws JSONException {

        final String expected =
                "{" +
                        "'name' : 'Toto'," +
                        "'id' : '123456'" +
                "}";
        final  String actual =
                "{" +
                        "'id' : '123456'," +
                        "'name' : 'Toto'" +
                "}";
        JSONAssert.assertEquals(expected, actual, JSONCompareMode.STRICT);
    }

    @Test
    public void should_be_equals_when_adding_other_fields_and_lenient_mode_test() throws JSONException {

        final String expected =
                "{" +
                        "'id' : '123456'," +
                        "'name' : 'Toto'" +
                "}";
        final  String actual =
                "{" +
                        "'id' : '123456'," +
                        "'name' : 'Toto'," +
                        "'age': 27" +
                "}";
        JSONAssert.assertEquals(expected, actual, JSONCompareMode.LENIENT);
    }

    @Test
    public void should_be_equals_when_is_different_array_order_and_lenient_mode_test() throws JSONException {

        final String expected =
                "{" +
                        "'array' : [1, 2, 3, 4]" +
                "}";
        final  String actual =
                "{" +
                        "'array' : [4, 3, 2, 1]" +
                "}";
        JSONAssert.assertEquals(expected, actual, JSONCompareMode.LENIENT);
    }

    @Test
    public void should_not_be_equals_when_is_different_array_order_and_strict_mode_test() throws JSONException {

        final String expected =
                "{" +
                        "'array' : [1, 2, 3, 4]" +
                "}";
        final  String actual =
                "{" +
                        "'array' : [4, 3, 2, 1]" +
                "}";
        JSONAssert.assertNotEquals(expected, actual, JSONCompareMode.STRICT);
    }
}

Find the source code of this example on GitHub.

3. Conclusion

Writing JSON unit tests was always a nightmare for me, as it requires a lot of code and efforts, Therefore JSONAssert may save you a lot of time.

 

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of