
And the great thing about this is that the diff document can then be merged with the first JSON document that has been compared, yielding the second JSON document that has been compared. The approach described in the other post focus in producing a JSON document that represents the differences between the two documents that have been compared.
#Junit json compare how to#
I recently put together another post describing how to compare JSON documents using JSON-P. The way that Map.equals () works is by comparing keys and values using the Object.equals () method. Then we’re using Map.equals () to check if two HashMaps have the same entries. Here, we’re creating two HashMap objects and adding entries. Quoting the RFC 8259, the document that defines the JSON format (highlights are mine):Īn object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.Īn array is an ordered sequence of zero or more values. Lets write a Java program to demonstrates comparing two HashMap using Map.equals () method.

This comparison method doesn’t take into account the order of the properties of objects, but it does take into account the order of the elements in arrays. Assuming you’re using SBT on your project, include JUnit into the project by adding this dependency line to your build.sbt file: libraryDependencies + 'com.novocode' 'junit-interface' '0. It will produce the following output: Entries only on left Include the JUnit library in your project, and use it in the same way you’ve used it in Java projects, with a few minor changes. REST Assured bases its tests only on JSON and HTTP, which are language-independent technologies. The fact that the application under test is written in Java is irrelevant to REST Assured. println ( "\n\nEntries in common\n-" ) difference. All REST Assured tests send a network request to our application, get a response back, and compare it against a predetermined result. println ( "\n\nEntries differing\n-" ) difference. println ( "\n\nEntries only on right\n-" ) difference. Lets start with preparing the environment to. println ( "Entries only on left\n-" ) difference. JSON with Java, This chapter covers how to encode and decode JSON objects using Java programming language. difference ( leftFlatMap, rightFlatMap ) System. flatten ( rightMap ) MapDifference difference = Maps.

flatten ( leftMap ) Map rightFlatMap = FlatMapUtil. It uses the JSON Pointer notation defined in the RFC 6901 for the keys, so I can easily locate the values. Import import import import import import import public final class FlatMapUtil
