有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java中的单元测试

我必须为这个java程序添加测试。但是,我不明白在这样的程序中我可以使用什么样的测试。我真的看不到使用测试的任何好处,因为它不像查找一个数字是否为素数那样是一个程序

package utcn;

import javax.swing.*;
import java.sql.*;
import java.awt.event.*;

public class BorrowBook extends JFrame implements ActionListener {
    /**
     * title will be the label for "Enter a book title" message
     */
    JLabel title;
    /**
     * ttitle will be the field for introducing the title
     */
    JTextField ttitle;
    /**
     * btn_borrow is the button for borrowing a book
     */
    JButton btn_borrow;

    /**
     * This method will create a window for borrowing a book.
     */
    public BorrowBook() {
        super("BorrowBook");
        title = new JLabel("Enter a book title:");
        title.setBounds(20, 20, 200, 15);
        ttitle = new JTextField(20);
        ttitle.setBounds(130, 20, 220, 30);
        btn_borrow = new JButton("BorrowBook");
        btn_borrow.setBounds(220, 65, 100, 40);
        btn_borrow.addActionListener(this);
        setVisible(true);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        setSize(500, 150);

        setLayout(null);
        add(btn_borrow);

        add(title);
        add(ttitle);
    }

    /**
     * This method will be called when the button is pressed. The application
     * require for an book title. If the introduced title can be find in the
     * database, it will be displayed a success message, otherwise an error
     * message. Also, in the database, the nr_exempare will be decreased and the
     * nr_imprumuturi will be increased.
     */
    @Override
    public void actionPerformed(ActionEvent ex) {
        Connection conn = null;
        PreparedStatement pst = null;
        PreparedStatement pst1 = null;
        ResultSet rs = null;
        String title = ttitle.getText();
        conn = MySqlConnect.ConnectDB();
        try {
            pst = conn.prepareStatement("update carti set nr_exemplare=nr_exemplare-1 where nume_carte=? ");
            pst1 = conn.prepareStatement("update carti set nr_imprumuturi=nr_imprumuturi+1 where nume_carte=? ");
            pst.setString(1, ttitle.getText());
            pst1.setString(1, ttitle.getText());
            int i = pst.executeUpdate();
            int i1 = pst1.executeUpdate();
            if ((i > 0) && (i1 > 0)) {
                dispose();
                JOptionPane.showMessageDialog(null, "Your book has been borrowed!");
            } else {
                JOptionPane.showMessageDialog(null, "Invalid book title.", "Accse Denied", JOptionPane.ERROR_MESSAGE);

            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }

    }
}

共 (2) 个答案

  1. # 1 楼答案

    您将很难在此处添加测试,因为您的方法可以完成所有操作:

    • 从UI获取一些文本属性
    • 建立数据库连接
    • 生成和执行查询
    • 给用户一些反馈

    通常,这些操作应该在负责各自操作集的多个类和对象之间分割

    一般来说,您可以并且应该在这里测试的是update语句是相应地执行的,并且新数据是您期望的数据。但是为了能够正确地测试它,您必须更好地组织代码

    从业务逻辑和数据库操作中分离UI

  2. # 2 楼答案

    测试应该检查你的逻辑。你不应该只考虑给出的正确数据。当用户输入一些奇怪的东西时会发生什么

    或者,当您继续开发代码时。让我们用另一个函数来改进它。您可以确保旧的东西仍然适用于已经编写的测试

    或者,当另一个开发人员处理您的代码时,还没有阅读您所有的优秀作品(以防代码稍长一点),他可以确保没有中断任何内容

    因此,在您的特殊情况下,我建议您在输入错误时测试是否显示正确的错误。或者一本书被借了两次会发生什么?诸如此类;)