在java中进行单元测试

时间:2016-01-01 19:47:23

标签: java unit-testing junit

我必须为这个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 个答案:

答案 0 :(得分:5)

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

  • 从UI获取一些文本属性
  • 建立数据库连接
  • 构建并执行查询
  • 提供一些用户反馈

通常,这些操作应该分成多个类和对象来负责它们各自的操作。

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

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

答案 1 :(得分:1)

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

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

或者当其他开发人员处理您的代码并且没有阅读您所有出色的工作时(如果代码有点长),他可以确保他没有制动任何东西。

因此,在您的特定情况下,我会建议在给出错误输入时测试是否显示正确的错误。或者,当一本书被借两次时会发生什么?这样的东西;)