我正在构建一个基于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);
}
});
}
}
Internet上似乎没有很多关于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向我推荐的第一个库,但是我也向其他功能测试库提出建议。
最佳答案
切换到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();
}
}