How do I execute a single test Swing component with AssertJ?

Are there any examples on how to test a single component or JComponent with AssertJ?

The getting started guide shows a weird example for testing an entire application with a main class that is not granular enough. I expect to test the custom components first.

UPDATE

Suppose I have the following component:

package tests;

import javax.swing.*;
import java.awt.*;

public class JCustomPanel extends JPanel {
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.setColor(Color.RED);
        g.fillRect(0, 0, getWidth(), getHeight());
    }
}

      

and I want to check if it displays in red. What to write?

package tests;

import org.assertj.swing.edt.FailOnThreadViolationRepaintManager;
import org.assertj.swing.fixture.JPanelFixture;
import org.assertj.swing.testing.AssertJSwingTestCaseTemplate;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class Test01 extends AssertJSwingTestCaseTemplate {

    private JPanelFixture panel;

    @BeforeClass
    public static void setUpOnce() {
        FailOnThreadViolationRepaintManager.install();
    }

    @Before
    public void setUp() {
        panel = new JPanelFixture(robot(), new JCustomPanel());
    }

    @Test
    public void testColorIsRed() {
        //what to write here?
    }
}

      

+3


source to share


2 answers


Check the mailing list there for quick answers to these specific questions, as there may only be a handful of AssertJ Swing users there.

There's a sample project that's not very big, but should be a good reference for new users on how to use AssertJ Swing.

Unfortunately, the "application" you are trying to test is one AssertJ Swing does not seek. Since it just appears to contain color, you don't have many options. AssertJ Swing is intended to be used as a functional testing tool, not a precise pixel testing tool. So it doesn't really have good support for creating pixel tests.



For color JPanelFixture

provides foreground()

and background()

to do something like:

panel.background().requireEqualTo(Color.RED);

      

But as said, this is not the main purpose of the library.

+2


source


I use assertj-swing a lot and I think you cannot test it. - in the foreground () from any device its component is actually called. Therefore, they do not help in your case.



Another option is to write some paint messages to your custom paint component and check the messages. it may be too ugly, but it works.

-2


source







All Articles