1

我正在构建一个简单的基于 Swing 的 Java 应用程序,并想添加一些简单的端到端测试。我遇到了UISpec4J库并将其导入到我的 Maven 项目中,如下所示:

<dependency>
    <groupId>org.uispec4j</groupId>
    <artifactId>uispec4j</artifactId>
    <version>2.4</version>
    <scope>test</scope>
</dependency>

我还有以下主要课程:

public class Main
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                MainWindow window = new MainWindow();
                window.setLocationRelativeTo(null);
                window.setVisible(true);
            }
        });
    }
}

互联网上似乎没有很多关于 UISpec4J 的材料,但根据我设法找到的一些教程,以下代码应该可以工作:

public class CanFillInABasicFormTest extends UISpecTestCase
{
    @BeforeClass
    public void setUp() {
        setAdapter(new MainClassAdapter(Main.class, new String[0]));
    }

    @Test
    public void test() 
    {
        Window mainWindow = getMainWindow();
    }
}

但相反,它失败了,但出现以下异常:

java.lang.AbstractMethodError: org.uispec4j.interception.toolkit.UISpecToolkit.createKeyboardFocusManagerPeer(Ljava/awt/KeyboardFocusManager;)Ljava/awt/peer/KeyboardFocusManagerPeer;
    at java.awt.KeyboardFocusManager.initPeer(Unknown Source)
    at java.awt.KeyboardFocusManager.<init>(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.<init>(Unknown Source)
    at java.awt.KeyboardFocusManager.getCurrentKeyboardFocusManager(Unknown Source)
    at java.awt.KeyboardFocusManager.getCurrentKeyboardFocusManager(Unknown Source)
    at javax.swing.UIManager.initialize(Unknown Source)
    at javax.swing.UIManager.maybeInitialize(Unknown Source)
    at javax.swing.UIManager.getDefaults(Unknown Source)
    at javax.swing.UIManager.put(Unknown Source)
    at org.uispec4j.interception.ui.UISpecLF.init(UISpecLF.java:11)
    at org.uispec4j.UISpec4J.init(UISpec4J.java:32)
    at org.uispec4j.UISpecTestCase.<clinit>(UISpecTestCase.java:31)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at junit.framework.TestSuite.createTest(TestSuite.java:61)
    at junit.framework.TestSuite.addTestMethod(TestSuite.java:294)
    at junit.framework.TestSuite.addTestsFromTestCase(TestSuite.java:150)
    at junit.framework.TestSuite.<init>(TestSuite.java:129)
    at org.junit.internal.runners.JUnit38ClassRunner.<init>(JUnit38ClassRunner.java:71)
    at org.junit.internal.builders.JUnit3Builder.runnerForClass(JUnit3Builder.java:14)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我究竟做错了什么?此外,UISpec4J 库是 Google 向我推荐的第一个库,但我也愿意向其他功能测试库提出建议。

4

2 回答 2

2

切换到 jdk1.6 update 0 对我有用。我使用的是 jdk1.7,我得到了同样的错误。

此外,我认为注释不是必需的,因为您使用的是 junit3(扩展 UISpecTestCase,扩展了 TestCase)

public class CanFillInABasicFormTest extends UISpecTestCase {
    public void setUp() {
        setAdapter(new MainClassAdapter(Main.class, new String[0]));
    }

    public void test() {
        Window mainWindow = getMainWindow();
    }
}
于 2013-12-23T10:30:38.993 回答
1

问题出在类org.uispec4j.interception.toolkit.UISpecToolkit中,它是在 java 1.7 之前创建的。UISpecToolkit类实现抽象类org.uispec4j.interception.toolkit.ToolkitDelegateToolkitDelegate扩展了另一个抽象类SunToolkitSunToolkit类是问题的根源。在 Java 1.7 中有一个新的抽象方法:

KeyboardFocusManagerPeer sun.awt.SunToolkit.getKeyboardFocusManagerPeer()

此方法未在 UISpecToolkit 类中实现。在Java 1.7 中的SunToolkit中也是新方法:

protected abstract boolean syncNativeQueue(final long timeout);

不可能轻松地将这个SunToolkit的新实现添加到 uispec4j。因此,如果没有新的库发布或一些不平凡的努力,就不可能在 java 1.7 中使用 uispec4j 库。

于 2014-10-31T09:51:01.413 回答