我无法使用AssertJ库在应用程序中测试GUI。为了不传播您程序的所有代码,编写了一个测试程序以说明问题的实质。
package AssertJ;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SomeFrame extends JFrame {
JFrame frame;
JLabel label1;
JButton button1;
public SomeFrame() {
createGUI();
}
public void createGUI() {
frame = new JFrame("EspiaServer");
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
label1 = new JLabel("Random");
button1 = new JButton("Click Me!");
button1.setName("button");
button1.setSize(200, 200);
button1.addActionListener(listener);
frame.add(label1);
frame.add(button1);
frame.pack();
}
public static void main(String[] args) {
new SomeFrame();
}
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int i = (int) (Math.random() * 99999) + 0;
label1.setText(String.valueOf(i));
}
};
}
这是测试:
package AssertJ;
import org.assertj.swing.edt.GuiActionRunner;
import org.assertj.swing.fixture.FrameFixture;
import org.junit.Before;
import org.junit.Test;
public class SomeFrameTest {
private FrameFixture window;
@Before
public void setUp() {
SomeFrame frame = GuiActionRunner.execute(() -> new SomeFrame());
window = new FrameFixture(frame);
window.show(); // shows the frame to test
}
@Test
public void createGUI() {
window.button("button").click();
}
}
就这样。应用程序的本质。在框架中,有一个按钮,当按下该按钮时,Jlabel会将其值更改为随机数。应用程序启动,一切都很好。但是当我运行测试时。他创建了该应用程序的gui,并在左上角创建了另一个窗口,该窗口不断闪烁和抽动。因此,命令window.button ("button").click(); is not satisfied.
几秒钟后,应用程序将引发异常:
rg.assertj.swing.exception.ComponentLookupException: Unable to find component using matcher org.assertj.swing.core.NameMatcher[name='button', type=javax.swing.JButton, requireShowing=true].
Component hierarchy:
AssertJ.SomeFrame[name='frame0', title='', enabled=true, visible=true, showing=true]
javax.swing.JRootPane[]
javax.swing.JPanel[name='null.glassPane']
javax.swing.JLayeredPane[]
javax.swing.JPanel[name='null.contentPane']
at org.assertj.swing.core.BasicComponentFinder.componentNotFound(BasicComponentFinder.java:287)
at org.assertj.swing.core.BasicComponentFinder.find(BasicComponentFinder.java:272)
at org.assertj.swing.core.BasicComponentFinder.find(BasicComponentFinder.java:265)
at org.assertj.swing.core.BasicComponentFinder.findByName(BasicComponentFinder.java:200)
at org.assertj.swing.fixture.AbstractContainerFixture.findByName(AbstractContainerFixture.java:641)
at org.assertj.swing.fixture.AbstractContainerFixture.button(AbstractContainerFixture.java:143)
at AssertJ.SomeFrameTest.createGUI(SomeFrameTest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
答案 0 :(得分:1)
在window.show()之后写window.maximize()对我有用。
答案 1 :(得分:0)
您的问题是您有两个 JFrame
:
SomeFrame
是JFrame
(SomeFrame extends JFrame
)SomeFrame
包含一个JFrame
(JFrame frame;
)测试代码的结构方式期望button("button")
包含在SomeFrame
JFrame对象中,但是您将其添加到frame
JFrame对象中。
您可以通过消除JFrame frame
实例变量来解决此问题:
public class SomeFrame extends JFrame {
JLabel label1;
JButton button1;
public SomeFrame() {
super("EspiaServer");
createGUI();
}
public void createGUI() {
setLayout(new FlowLayout());
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
label1 = new JLabel("Random");
button1 = new JButton("Click Me!");
button1.setName("button");
button1.setSize(200, 200);
button1.addActionListener(listener);
add(label1);
add(button1);
pack();
}
// some code left out
}