3

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

Lampapos
  • 1,063
  • 1
  • 12
  • 26
  • Possible duplicate of http://stackoverflow.com/questions/91179/automated-tests-for-java-swing-guis – beny23 Aug 17 '11 at 13:25
  • I don't have any experience with it, but the [approval test library](http://approvaltests.sourceforge.net/) could very much be able to help you here. – Joachim Sauer Aug 17 '11 at 13:25

2 Answers2

1

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

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • Say I have a drawable object with draw(graphics)/getColor()/setColor(color), I want to test that the draw call will draw a red object whenever I have done setColor(Color.Red). This isn't the same as checking getColor().equals(Color.Red). It requires more than that, for example the unit test calling the draw() method with a mock graphics that actually detects the object actually drawing itself with the required color. Way more difficult than testing that mutating an object actually mutates it... – Anders Forsgren Aug 17 '11 at 13:35
  • So we can only test model of component? And if we try to test View of this component we should create complex mock-object with draw method which can check actual view of component. Am I right? – Lampapos Aug 17 '11 at 13:49
  • @Anders - if you want to *unit test* a `paint(Graphics g)` implementation, then you could pass an image type graphics and verify, that the painted image is as expected (If you really want to do that). – Andreas Dolk Aug 17 '11 at 21:16
  • @Andreas, That is one possibility. Feels a bit difficult to do image recognition though, I'd probably settle with testing mockGraphics.getThingsDrawn().count() == 1 and its color is red so on. Lampapos:, what we are tryinhg to say is that it is difficult to test how something *looks*, you can ask something how it (thinks) it looks easily, but that is only almost the same thing. – Anders Forsgren Aug 17 '11 at 21:19
1

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.

Alistair A. Israel
  • 6,417
  • 1
  • 31
  • 40