Shazamcrest

Library that extends the functionality of hamcrest

License

License

GroupId

GroupId

com.shazam
ArtifactId

ArtifactId

shazamcrest
Last Version

Last Version

0.11
Release Date

Release Date

Type

Type

jar
Description

Description

Shazamcrest
Library that extends the functionality of hamcrest
Project URL

Project URL

http://github.com/shazam/shazamcrest
Source Code Management

Source Code Management

http://github.com/shazam/shazamcrest

Download shazamcrest

How to add to project

<!-- https://jarcasting.com/artifacts/com.shazam/shazamcrest/ -->
<dependency>
    <groupId>com.shazam</groupId>
    <artifactId>shazamcrest</artifactId>
    <version>0.11</version>
</dependency>
// https://jarcasting.com/artifacts/com.shazam/shazamcrest/
implementation 'com.shazam:shazamcrest:0.11'
// https://jarcasting.com/artifacts/com.shazam/shazamcrest/
implementation ("com.shazam:shazamcrest:0.11")
'com.shazam:shazamcrest:jar:0.11'
<dependency org="com.shazam" name="shazamcrest" rev="0.11">
  <artifact name="shazamcrest" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.shazam', module='shazamcrest', version='0.11')
)
libraryDependencies += "com.shazam" % "shazamcrest" % "0.11"
[com.shazam/shazamcrest "0.11"]

Dependencies

compile (5)

Group / Artifact Type Version
org.skyscreamer : jsonassert jar 1.2.3
com.google.guava : guava jar 17.0
com.google.code.gson : gson jar 2.3.1
junit : junit jar 4.11
org.apache.commons : commons-lang3 jar 3.1

provided (2)

Group / Artifact Type Version
org.hamcrest : hamcrest-core jar 1.3
org.hamcrest : hamcrest-library jar 1.3

Project Modules

There are no modules declared in this project.

Shazamcrest

'Shazamcrest' is a library that extends the functionality of hamcrest.

Assertions on complete beans are made simpler by serialising the actual and expected beans to json, and comparing the two. The diagnostics are leveraging the comparison functionality of IDEs like Eclipse or IntelliJ.

Usage

Having a Person bean with the following structure:

Person person
    |-- String name
    |-- String surname
    |-- Address address
        |-- String streetName
        |-- int streetNumber
        |-- String postcode

to compare two Person beans with Shazamcrest we would write:

assertThat(actualPerson, sameBeanAs(expectedPerson));

instead of explicitly match every field of the bean and sub-beans:

assertThat(actualPerson, allOf(
        hasProperty("name", equalTo(expectedPerson.name)),
        hasProperty("surname", equalTo(expectedPerson.surname)),
        hasProperty("address", allOf(
            hasProperty("streetName", equalTo(expectedPerson.address.streetName)),
            hasProperty("streetNumber", equalTo(expectedPerson.address.streetNumber)),
            hasProperty("postcode", equalTo(expectedPerson.address.postcode)))
        )
    ));

Error Messages

If the person address streetName does not match the expectations, the following diagnostic message is displayed:

org.junit.ComparisonFailure: address.streetName
        Expected: Via Roma
    got: Via Veneto
        expected:<... "streetName": "Via [Roma]",
    "streetNumber...> but was:<... "streetName": "Via [Veneto]",
    "streetNumber...>

The exception thrown is a ComparisonFailure which can be used by IDEs like Eclipse and IntelliJ to display a visual representation of the differences.

Comparison failure diagnostic

Note: in order to get the ComparisonFailure on mismatch the "assertThat" to use is com.shazam.shazamcrest.MatcherAssert.assertThat rather than org.hamcrest.MatcherAssert.assertThat

Ignoring fields

If we are not interested in matching the street name, we can ignore it by specifying the field path:

assertThat(actualPerson, sameBeanAs(expectedPerson).ignoring("address.streetName"));

If we want to match the address only by the postcode, we can ignore street name and number by specifying the fields name pattern:

assertThat(actualPerson, sameBeanAs(expectedPerson).ignoring(startsWith("street")));

where startsWith is an Hamcrest matcher.

Custom matching

If we want to make sure that the street name starts with "Via" at least:

assertThat(actualPerson, sameBeanAs(expectedPerson).with("address.streetName"), startsWith("Via"));

Circular references

Having a Shop bean with the following structure:

Shop shop
	|-- String name
    |-- Store store
        |-- Boss boss
            |-- Clerk clerk
                |-- Store store
                |-- Boss boss

Comparing two Shop objects throws a StackOverflowError, because of the cycles Clerk -> Store -> Boss -> Clerk and Clerk -> Boss -> Clerk.

From version 0.10 the circular reference is detected automatically and the serialiser is instructed to serialise the instance once and replace all the other occurrences with a pointer:

assertThat(actualShop, sameBeanAs(expectedShop));

produces the following representation:

{
  "store": {
    "0x1": {
      "0x1": {
        "0x1": {
          "boss": "0x2"
        }
      }
    },
    "0x2": {
      "0x1": {
        "0x1": {
          "clerk": {
            "boss": "0x2",
            "store": "0x1"
          }
        }
      }
    }
  },
  "name": "shop"
}

QuickStart

To use, download the zip or add the following to your project's pom.xml:

<dependency>
    <groupId>com.shazam</groupId>
    <artifactId>shazamcrest</artifactId>
    <version>0.11</version>
</dependency>
com.shazam

Shazam

Versions

Version
0.11
0.10
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1