Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit-testing of visual components in Java

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

like image 800
Lampapos Avatar asked Sep 27 '26 22:09

Lampapos


2 Answers

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).

like image 87
Andreas Dolk Avatar answered Sep 30 '26 12:09

Andreas Dolk


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.

like image 27
Alistair A. Israel Avatar answered Sep 30 '26 12:09

Alistair A. Israel