我正在构建一个简单的基于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向我推荐的第一个库,但我也向其他功能测试库提出了建议。
答案 0 :(得分:1)
切换到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();
}
}
答案 1 :(得分:0)
问题出现在java 1.7之前创建的类<em> org.uispec4j.interception.toolkit.UISpecToolkit 中。类 UISpecToolkit 实现抽象类 org.uispec4j.interception.toolkit.ToolkitDelegate 。 ToolkitDelegate 扩展了另一个抽象类 SunToolkit 。类 SunToolkit 是问题的根源。在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库。