You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

简单GUI登录页面的JUnit测试实现方案咨询

解决方案

现有测试问题梳理

  • 用户名密码填写颠倒:现有测试中将用户名John赋值给了密码框,密码123123赋值给了用户名框,和业务逻辑的校验条件完全不符
  • 断言无实际校验意义:现有测试写的assertEquals("Welcome to junit.","Welcome to junit.");是恒成立的判断,完全没有校验GUI组件的实际返回结果
  • 可选优化:Swing组件的渲染更新依赖事件调度线程(EDT),如果测试出现组件内容未更新的情况,可以添加100ms以内的线程等待,或者用SwingUtilities.invokeAndWait包裹组件操作逻辑

完整测试实现方案

以下实现基于JUnit 5,测试执行前会自动清理历史日志文件,避免历史数据干扰测试结果:

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class LoginTest {
    private GUI gui;
    private static final String LOG_PATH = "Log.txt";

    @BeforeEach
    void init() {
        // 每次测试前初始化GUI实例
        gui = new GUI();
        // 删除历史日志文件
        File logFile = new File(LOG_PATH);
        if (logFile.exists()) {
            logFile.delete();
        }
    }

    @AfterEach
    void tearDown() {
        // 测试后清理日志文件
        File logFile = new File(LOG_PATH);
        if (logFile.exists()) {
            logFile.delete();
        }
    }

    // 测试用例1:登录成功场景
    @Test
    void testLoginSuccess() {
        gui.userText.setText("John");
        gui.passwordText.setText("123123");
        gui.loginButton.doClick();

        assertEquals("Log in successful", gui.success.getText());
        assertEquals("", gui.failure.getText());
    }

    // 测试用例2:登录失败场景
    @Test
    void testLoginFail() {
        gui.userText.setText("WrongUser");
        gui.passwordText.setText("WrongPwd");
        gui.loginButton.doClick();

        assertEquals("Incorect, try again", gui.failure.getText());
        assertEquals("", gui.success.getText());
    }

    // 测试用例3:清除按钮功能正常
    @Test
    void testClearButton() {
        // 先给输入框赋值
        gui.userText.setText("testUser");
        gui.passwordText.setText("testPwd");
        // 点击清除按钮
        gui.clearButton.doClick();
        // 校验两个输入框都已清空
        assertEquals("", gui.userText.getText());
        assertEquals("", gui.passwordText.getText());
    }

    // 测试用例4:日志文件正常生成
    @Test
    void testLogFileGenerate() throws IOException {
        // 触发一次成功登录
        gui.userText.setText("John");
        gui.passwordText.setText("123123");
        gui.loginButton.doClick();

        // 校验日志文件存在
        File logFile = new File(LOG_PATH);
        assertTrue(logFile.exists());

        // 校验日志内容正确
        BufferedReader reader = new BufferedReader(new FileReader(logFile));
        String lastLine = "";
        String currentLine;
        while ((currentLine = reader.readLine()) != null) {
            lastLine = currentLine;
        }
        reader.close();
        assertTrue(lastLine.contains("User - John-login successful."));
    }
}

业务代码可优化点(可选)

当前logInSucccess方法中创建文件的逻辑存在错误:

原代码判断逻辑为if (f.exists() && !f.isDirectory()) { f.createNewFile(); },只有当文件已经存在时才会创建新文件,完全不符合预期,建议修改为:

if (!f.exists()) {
    f.createNewFile();
}

内容的提问来源于stack exchange,提问作者Sergey V.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 07:27:01