Lampapos
Lampapos

Reputation: 1081

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

Upvotes: 3

Views: 1233

Answers (2)

Alistair A. Israel
Alistair A. Israel

Reputation: 6567

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.

Upvotes: 1

Andreas Dolk
Andreas Dolk

Reputation: 114807

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

Upvotes: 1

Related Questions