I have a method that deals with an image. The method takes one image, performs some manipulations over it and returns another image:
public BufferedImage manipulateImage (Image image) {
...
}
I'm not sure if there are any best practices of writing unit tests for such activities. What characteristics of the image should be checked at a first place? How to check if the image was not spoiled? For instance, once I faced a problem when GIF images became color-inverted after reading them with ImageIO
and saving back.
If you want true Unit Tests, then you have to mock the cache: write a mock object that implements the same interface as the cache, but instead of being a cache, it keeps track of the calls it receives, and always returns what the real cache should be returning according to the test case.
Get your original image (x
), run the transform and save the manipulated image (y
), physically check yourself that y
is what you want to test. Your test is then that F(x) = y
, if you have both x
and y
in your src/test/resources
directory you can compare y
to the output of your test.
You may also be interested in http://pdiff.sourceforge.net/ (C++ not Java) should you not need to test for 100% equality.
Edit: also see this question - Image comparison - fast algorithm
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With