6

I have a disabled JTable that provides a popup menu:

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTable;

public class DisabledTableFrame extends JFrame {

    public DisabledTableFrame() {

        setSize(200, 100);
        setTitle(getClass().getCanonicalName());
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        JTable table = new JTable();
        add(table);

        table.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                mouseReleased(e);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                new PopupMenu();
            }

        });

        table.setEnabled(false);
        setVisible(true);
    }

    public static void main(String[] args) {
        new DisabledTableFrame();

    }

    private class PopupMenu extends JPopupMenu {

        public PopupMenu() {

            JMenuItem menuItem = new JMenuItem("TEST");
            add(menuItem);
            setVisible(true);
        }
    }
}

So when testing this function with AssertJ Swing using:

import org.assertj.swing.edt.GuiActionRunner;
import org.assertj.swing.edt.GuiQuery;
import org.assertj.swing.fixture.FrameFixture;
import org.assertj.swing.junit.testcase.AssertJSwingJUnitTestCase;
import org.junit.Test;

public class PopupTestCase extends AssertJSwingJUnitTestCase {

    protected FrameFixture window;

    @Override
    protected void onSetUp() {
        DisabledTableFrame mainFrame = GuiActionRunner
                .execute(new GuiQuery<DisabledTableFrame>() {
                    protected DisabledTableFrame executeInEDT() {

                        return new DisabledTableFrame();
                    }
                });

        window = new FrameFixture(robot(), mainFrame);
    }

    @Test
    public void popupShouldBeOpened() {

        window.table().showPopupMenu();
    }
}

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>AssertJSwingInactiveTest</groupId>
    <artifactId>AssertJSwingInactiveTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-swing-junit-4.5</artifactId>
            <version>1.2.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

It works fine when the table is enabled. But the popupShoudBeOpened() test on the disabled table throws the following exception:

   java.lang.IllegalStateException: Expecting component javax.swing.JTable[name=null, rowCount=0, columnCount=0, enabled=false, visible=true, showing=true] to be enabled
at org.assertj.swing.driver.ComponentPreconditions.checkEnabled(ComponentPreconditions.java:68)
at org.assertj.swing.driver.ComponentPreconditions.checkEnabledAndShowing(ComponentPreconditions.java:48)
at org.assertj.swing.driver.ComponentDriver$2.executeInEDT(ComponentDriver.java:555)
at org.assertj.swing.edt.GuiTask.run(GuiTask.java:38)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:312)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:745)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:715)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
at org.assertj.swing.edt.GuiActionRunner.execute(GuiActionRunner.java:103)
at org.assertj.swing.driver.ComponentDriver.checkInEdtEnabledAndShowing(ComponentDriver.java:552)
at org.assertj.swing.driver.ComponentDriver.invokePopupMenu(ComponentDriver.java:519)
at org.assertj.swing.fixture.AbstractJPopupMenuInvokerFixture.showPopupMenu(AbstractJPopupMenuInvokerFixture.java:95)
at GUITestCase.popupShouldBeOpened(GUITestCase.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

As the popup menu obviously works even on the disabled table, how can I get AssertJ to "right click" the disabled table?

4

1 回答 1

1

AsserJ 期望启用测试的组件。
一旦它被禁用,它将抛出一个 IllegalStateException。在此处
检查 AbstractComponentFixture API 。

解决此问题的一种方法是划分 2 个测试并包含预期禁用表时的异常:

@Test
public void popupShouldBeOpenedIfTableIsDisabled() {
    try {
        window.table().showPopupMenu();
    } catch (IllegalStateException e) {
        // Continue normally if IllegalStateException was thrown since the table is disabled on purpose.
    }
}

@Test
public void popupShouldBeOpenedIfTableIsEnabled() {
    window.table().showPopupMenu();
    // IllegalStateException will fail the test since the table is enabled.
}
于 2015-12-03T08:01:03.123 回答