Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Unit testing image manipulations

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.

like image 706
Denis Kniazhev Avatar asked Sep 10 '10 15:09

Denis Kniazhev


People also ask

What are best practices for unit testing methods that use cache heavily?

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.


1 Answers

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

like image 167
Jon Freedman Avatar answered Sep 28 '22 07:09

Jon Freedman