I am working on part of a Java application that takes an image as a byte array, reads it into a java.awt.image.BufferedImage
instance and passes it to a third-party library for processing.
For a unit test, I want to take an image (from a file on disk) and assert that it is equal to the same image that has been processed by the code.
- My expected
BufferedImage
is read from a PNG file on disk usingImageIO.read(URL)
. - My test code reads the same file into a
BufferedImage
and writes that to a byte array as PNG to provide to the system under test.
When the system under test writes the byte array to a new BufferedImage
I want to assert that the two images are equal in a meaningful way. Using equals()
(inherited from Object
) doesn’t work (of course). Comparing BufferedImage.toString()
values also doesn’t work because the output string includes object reference information.
Does anybody know any shortcuts? I would prefer not to bring in a third-party library for a single unit test in a small part of a large application.
working well but not efficient
I can't think of anything besides a brute force "do loop":