GithubHelp home page GithubHelp logo

Comments (3)

lukas-krecan-gdc avatar lukas-krecan-gdc commented on August 16, 2024

Can you please post here a code snippet of your test so I know how to reproduce it?

from jsonunit.

LomaPL avatar LomaPL commented on August 16, 2024

I can only provide a simplified example as the API that returns the response is company internal.

We expect the following JSON:

{
    "response":[
        {
            "empolyees":[
                {
                    "firstName":"Joe",
                    "lastName":"Doe",
                    "dob":"1987-03-21"
                },
                {
                    "firstName":"Jason",
                    "lastName":"Kowalski",
                    "dob":"1986-02-12",
                },
                {
                    "firstName":"Kate",
                    "lastName":"Smith",
                    "dob":"1985-01-11",
                }
            ],
            "attributes":null,
            "id":123
        }

Server returns employees in different order and now we simply assert whether the expected JSON is equal to actual.

    @Test
    public void testJSON() {

        final String actual = "{\"response\":[{\"attributes\":null,\"empolyees\":[{\"dob\":\"1987-03-21\",\"firstName\":\"Joe\",\"lastName\":\"Doe\"},{\"dob\":\"1986-02-12\",\"firstName\":\"Jason\",\"lastName\":\"Kowalski\"},{\"dob\":\"1985-01-11\",\"firstName\":\"Kate\",\"lastName\":\"Smith\"}],\"id\":123}]}";
        final String expected = "{\"response\":[{\"attributes\":null,\"empolyees\":[{\"dob\":\"1985-01-11\",\"firstName\":\"Kate\",\"lastName\":\"Smith\"},{\"dob\":\"1986-02-12\",\"firstName\":\"Jason\",\"lastName\":\"Kowalski\"},{\"dob\":\"1987-03-21\",\"firstName\":\"Joe\",\"lastName\":\"Doe\"}],\"id\":123}]}";

        assertThatJson(actual).isEqualTo(expected).when(Option.IGNORING_ARRAY_ORDER);
    }

from jsonunit.

lukas-krecan avatar lukas-krecan commented on August 16, 2024

If you specify when before the condition it works

@Test
public void testArrayShouldMatchRegardlessOfOrder() {

    final String actual = "{\"response\":[{\"attributes\":null,\"empolyees\":[{\"dob\":\"1987-03-21\",\"firstName\":\"Joe\",\"lastName\":\"Doe\"},{\"dob\":\"1986-02-12\",\"firstName\":\"Jason\",\"lastName\":\"Kowalski\"},{\"dob\":\"1985-01-11\",\"firstName\":\"Kate\",\"lastName\":\"Smith\"}],\"id\":123}]}";
    final String expected = "{\"response\":[{\"attributes\":null,\"empolyees\":[{\"dob\":\"1985-01-11\",\"firstName\":\"Kate\",\"lastName\":\"Smith\"},{\"dob\":\"1986-02-12\",\"firstName\":\"Jason\",\"lastName\":\"Kowalski\"},{\"dob\":\"1987-03-21\",\"firstName\":\"Joe\",\"lastName\":\"Doe\"}],\"id\":123}]}";

    assertThatJson(actual).when(Option.IGNORING_ARRAY_ORDER).isEqualTo(expected);
}

I admit that it's not intuitive and the API should not allow the incorrect combination. If you think about it, in Java isEqualMethod can not know that there will be another method call in the chain so it has to do the comparison at once. I will try to stress it in the documentation.

from jsonunit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.