Assume that we have some simple Java visual component, for example, rectangle. Component can change position (let it be changePosition(int x, int y) method) and color (changeColor(Color color)). How can I create unit -test for such component?
This is extremely simple situation, but, I think, it is nice illustration for my question: how we can create unit-test for any visual component in Java?
Thanks in advance
I strongly doubt, that a component can change its position or change its color. Components have properties that are used by rendering engines to display them on a screen.
A visual component is some sort of model for something that we see on a display.
So when we call changePosition(x,y) on a component, then we usually alter field values on the component instance. And we only have to (unit-)test, if those fields have the expected values after we called changePosition(x,y).
I would write up a test that creates a suitable BufferedImage, retrieve that image's graphics context and instruct the component (under test) to paint() on that.
Then I could perform some assertions on the rendered image, such as checking if a particular pixel/region is in the desired color.
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