Class ImageAssert


public class ImageAssert extends GenericAssert<BufferedImage>
Understands assertion methods for images. To create a new instance of this class use the method Assertions.assertThat(BufferedImage).
  • Field Details

    • ZERO_THRESHOLD

      private static final Threshold ZERO_THRESHOLD
    • imageReader

      private static ImageReader imageReader
  • Constructor Details

    • ImageAssert

      protected ImageAssert(BufferedImage actual)
      Creates a new ImageAssert.
      Parameters:
      actual - the target to verify.
  • Method Details

    • read

      public static BufferedImage read(String imageFilePath) throws IOException
      Reads the image in the specified path.
      Parameters:
      imageFilePath - the path of the image to read.
      Returns:
      the read image.
      Throws:
      NullPointerException - if the given path is null.
      IllegalArgumentException - if the given path does not belong to a file.
      IOException - if any I/O error occurred while reading the image.
    • as

      public ImageAssert as(String description)
      Sets the description of the actual value, to be used in as message of any AssertionError thrown when an assertion fails. This method should be called before any assertion method, otherwise any assertion failure will not show the provided description.

      For example:

       assertThat(val).as("name").isEqualTo("Frodo");
       

      Specified by:
      as in class GenericAssert<BufferedImage>
      Parameters:
      description - the description of the actual value.
      Returns:
      this assertion object.
    • describedAs

      public ImageAssert describedAs(String description)
      Alias for GenericAssert.as(String), since "as" is a keyword in Groovy. This method should be called before any assertion method, otherwise any assertion failure will not show the provided description.

      For example:

       assertThat(val).describedAs("name").isEqualTo("Frodo");
       

      Specified by:
      describedAs in class GenericAssert<BufferedImage>
      Parameters:
      description - the description of the actual value.
      Returns:
      this assertion object.
    • as

      public ImageAssert as(Description description)
      Sets the description of the actual value, to be used in as message of any AssertionError thrown when an assertion fails. This method should be called before any assertion method, otherwise any assertion failure will not show the provided description.

      For example:

       assertThat(val).as(new BasicDescription("name")).isEqualTo("Frodo");
       

      Specified by:
      as in class GenericAssert<BufferedImage>
      Parameters:
      description - the description of the actual value.
      Returns:
      this assertion object.
    • describedAs

      public ImageAssert describedAs(Description description)
      Alias for GenericAssert.as(Description), since "as" is a keyword in Groovy. This method should be called before any assertion method, otherwise any assertion failure will not show the provided description.

      For example:

       assertThat(val).describedAs(new BasicDescription("name")).isEqualTo("Frodo");
       

      Specified by:
      describedAs in class GenericAssert<BufferedImage>
      Parameters:
      description - the description of the actual value.
      Returns:
      this assertion object.
    • satisfies

      public ImageAssert satisfies(Condition<BufferedImage> condition)
      Verifies that the actual image satisfies the given condition.
      Specified by:
      satisfies in class GenericAssert<BufferedImage>
      Parameters:
      condition - the given condition.
      Returns:
      this assertion object.
      Throws:
      NullPointerException - if the given condition is null.
      AssertionError - if the actual image does not satisfy the given condition.
      See Also:
    • doesNotSatisfy

      public ImageAssert doesNotSatisfy(Condition<BufferedImage> condition)
      Verifies that the actual image does not satisfy the given condition.
      Specified by:
      doesNotSatisfy in class GenericAssert<BufferedImage>
      Parameters:
      condition - the given condition.
      Returns:
      this assertion object.
      Throws:
      NullPointerException - if the given condition is null.
      AssertionError - if the actual image satisfies the given condition.
      See Also:
    • is

      public ImageAssert is(Condition<BufferedImage> condition)
      Specified by:
      is in class GenericAssert<BufferedImage>
      Parameters:
      condition - the given condition.
      Returns:
      this assertion object.
      Throws:
      NullPointerException - if the given condition is null.
      AssertionError - if the actual image does not satisfy the given condition.
      Since:
      1.2
    • isNot

      public ImageAssert isNot(Condition<BufferedImage> condition)
      Specified by:
      isNot in class GenericAssert<BufferedImage>
      Parameters:
      condition - the given condition.
      Returns:
      this assertion object.
      Throws:
      NullPointerException - if the given condition is null.
      AssertionError - if the actual image satisfies the given condition.
      Since:
      1.2
    • isEqualTo

      public ImageAssert isEqualTo(BufferedImage expected)
      Verifies that the actual image is equal to the given one. Two images are equal if they have the same size and the pixels at the same coordinates have the same color.
      Specified by:
      isEqualTo in class GenericAssert<BufferedImage>
      Parameters:
      expected - the given image to compare the actual image to.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual image is not equal to the given one.
    • isEqualTo

      public ImageAssert isEqualTo(BufferedImage expected, Threshold threshold)
      Verifies that the actual image is equal to the given one. Two images are equal if:
      1. they have the same size
      2. the difference between the RGB values of the color of each pixel is less than or equal to the given threshold
      Parameters:
      expected - the given image to compare the actual image to.
      threshold - the threshold to use to decide if the color of two pixels are similar: two pixels that are identical to the human eye may still have slightly different color values. For example, by using a threshold of 1 we can indicate that a blue value of 60 is similar to a blue value of 61.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual image is not equal to the given one.
      Since:
      1.1
    • failIfNull

      private void failIfNull(BufferedImage expected)
    • failIfNotEqual

      private void failIfNotEqual(Dimension a, Dimension e)
    • failIfNotEqualColor

      private void failIfNotEqualColor(BufferedImage expected, Threshold threshold)
    • failIfNotEqual

      private void failIfNotEqual(RGBColor a, RGBColor e, Threshold threshold, int x, int y)
    • isNotEqualTo

      public ImageAssert isNotEqualTo(BufferedImage image)
      Verifies that the actual image is not equal to the given one. Two images are equal if they have the same size and the pixels at the same coordinates have the same color.
      Specified by:
      isNotEqualTo in class GenericAssert<BufferedImage>
      Parameters:
      image - the given image to compare the actual image to.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual image is equal to the given one.
    • sizeOf

      private static Dimension sizeOf(BufferedImage image)
    • hasEqualColor

      private boolean hasEqualColor(BufferedImage expected)
    • isNotNull

      public ImageAssert isNotNull()
      Verifies that the actual image is not null.
      Specified by:
      isNotNull in class GenericAssert<BufferedImage>
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual image is null.
    • isNotSameAs

      public ImageAssert isNotSameAs(BufferedImage expected)
      Verifies that the actual image is not the same as the given one.
      Specified by:
      isNotSameAs in class GenericAssert<BufferedImage>
      Parameters:
      expected - the given image to compare the actual image to.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual image is the same as the given one.
    • isSameAs

      public ImageAssert isSameAs(BufferedImage expected)
      Verifies that the actual image is the same as the given one.
      Specified by:
      isSameAs in class GenericAssert<BufferedImage>
      Parameters:
      expected - the given image to compare the actual image to.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual image is not the same as the given one.
    • hasSize

      public ImageAssert hasSize(Dimension expected)
      Verifies that the size of the actual image is equal to the given one.
      Parameters:
      expected - the expected size of the actual image.
      Returns:
      this assertion object.
      Throws:
      AssertionError - if the actual image is null.
      NullPointerException - if the given size is null.
      AssertionError - if the size of the actual image is not equal to the given one.
    • overridingErrorMessage

      public ImageAssert overridingErrorMessage(String message)
      Replaces the default message displayed in case of a failure with the given one.

      For example, the following assertion:

       assertThat("Hello").isEqualTo("Bye");
       
      will fail with the default message "expected:<'[Bye]'> but was:<'[Hello]'>."

      We can replace this message with our own:

       assertThat("Hello").overridingErrorMessage("'Hello' should be equal to 'Bye'").isEqualTo("Bye");
       
      in this case, the assertion will fail showing the message "'Hello' should be equal to 'Bye'".

      Specified by:
      overridingErrorMessage in class GenericAssert<BufferedImage>
      Parameters:
      message - the given error message, which will replace the default one.
      Returns:
      this assertion.
    • imageReader

      static void imageReader(ImageReader newImageReader)