AssertJ Swing GUI测试配置报错:FrameFixture构造方法无法解析
问题原因
FrameFixture是Swing GUI测试框架(AssertJ Swing/旧版FEST Swing)提供的工具类,它的构造方法仅接收java.awt.Window及其子类(比如JFrame)作为入参- 你当前的
Login类没有继承JFrame,只是在内部封装了一个public JFrame frame成员变量,直接传Login实例会报构造方法不匹配的错误 - 不需要强行继承
SampleFrame,这个类不是Swing GUI测试的必需依赖,属于走偏的解决思路
修复方案
方案1:调整Login类继承结构(推荐)
直接让Login继承JFrame,不需要额外维护单独的frame成员,修改后代码如下:
package GUI; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPasswordField; import java.awt.Font; import javax.swing.JTextField; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; // 直接继承JFrame public class Login extends JFrame{ private JTextField textField; private JPasswordField textField_1; public Login() { initialize(); } private void initialize() { // 原来的frame变量替换成this即可 this.setBounds(100, 100, 450, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setTitle("Banking System"); this.getContentPane().setLayout(null); JLabel label = new JLabel("Banking System"); label.setFont(new Font("Tahoma", Font.BOLD, 17)); label.setBounds(147, 11, 151, 41); this.getContentPane().add(label); JLabel lblLoginScreen = new JLabel("Login Screen"); lblLoginScreen.setFont(new Font("Tahoma", Font.PLAIN, 13)); lblLoginScreen.setBounds(170, 63, 101, 23); this.getContentPane().add(lblLoginScreen); JLabel lblUsername = new JLabel("Username:"); lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 12)); lblUsername.setBounds(55, 119, 64, 23); this.getContentPane().add(lblUsername); JLabel lblPassword = new JLabel("Password:"); lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 12)); lblPassword.setBounds(55, 159, 64, 23); this.getContentPane().add(lblPassword); textField = new JTextField(); textField.setBounds(130, 121, 86, 20); this.getContentPane().add(textField); textField.setColumns(10); textField.setText("admin"); textField_1 = new JPasswordField(); textField_1.setBounds(130, 161, 86, 20); this.getContentPane().add(textField_1); textField_1.setColumns(10); JButton btnLogin = new JButton("Login"); btnLogin.addActionListener(new ActionListener() { @SuppressWarnings("deprecation") public void actionPerformed(ActionEvent e) { String user,pass; textField.setText("admin"); user="admin"; pass=textField_1.getText(); if((user.equals("admin")&&(pass.equals("admin")))) { JOptionPane.showMessageDialog(getComponent(0), "Login Successfully"); setVisible(false); GUIForm.menu.setVisible(true); } else { JOptionPane.showMessageDialog(getComponent(0), "Login Failed"); } } }); btnLogin.setBounds(260, 138, 89, 23); this.getContentPane().add(btnLogin); } }
修改后原有TestLogin代码无需改动即可正常运行。
方案2:不修改Login类,直接传入内部的frame成员
如果不想改动现有Login类的结构,仅修改TestLogin的初始化代码即可:
@Before public void setUp() { Login login = GuiActionRunner.execute(() -> new Login()); // 直接传入Login类里封装的JFrame成员 window = new FrameFixture(login.frame); window.show(); }
依赖补充
如果IntelliJ仍然提示FrameFixture类找不到,确认你已引入AssertJ Swing的测试依赖,Maven配置参考如下:
<dependency> <groupId>org.assertj</groupId> <artifactId>assertj-swing-junit</artifactId> <version>3.17.1</version> <scope>test</scope> </dependency>
内容的提问来源于stack exchange,提问作者Programmer2B
相关产品推荐
相关产品推荐

