简体   繁体   English

Java单元测试

[英]Unit testing in java

I have to add tests for this java program. 我必须为此Java程序添加测试。 But, i don't understand for what could i use tests in such a program. 但是,我不知道我可以在这种程序中使用什么测试。 I don't really see any benefits of using tests because it is not a program like finding if a number is or isn't prime. 我真的没有看到使用测试的任何好处,因为它不是一个像查找数字是否为质数的程序。

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);
        }

    }
}

You are going to have a very hard time adding tests here since your method does everything: 您将很难在这里添加测试,因为您的方法可以完成所有工作:

  • getting some text property from the UI 从UI获取一些文本属性
  • build a database connection 建立数据库连接
  • build and execute queries 建立并执行查询
  • give some user feedback 给一些用户反馈

Normally those operations should be split amongst multiple classes and objects responsible for their respective set of operations. 通常,这些操作应在负责各自操作集的多个类和对象之间划分。

In general what you could and should test here is that the update statements were executed accordingly and that the new data is the data you would expect. 通常,您可以并且应该在此处进行测试的是,更新语句已相应执行,并且新数据就是您期望的数据。 But to be able to test that properly you will have to organize your code a lot better. 但是要能够正确测试,您将不得不更好地组织代码。

Split the UI from the business logic from the database operations. 从数据库操作的业务逻辑中拆分UI。

Tests should, well, check your logic. 测试应该检查您的逻辑。 You shouldn't just think about correct data given. 您不应该只考虑给出的正确数据。 What happens when a user enters something weird? 用户输入奇怪的内容会怎样?

Or furthermore when you keep developing your code. 或者,当您继续开发代码时。 Let's say improve it by another function. 假设通过其他功能对其进行了改进。 You can make sure the old stuff still works with the already-written tests. 您可以确保旧内容仍然可以与已编写的测试一起使用。

Or when another developer works on your code and hasn't read all your great work (in case the code was a bit longer) he can make sure he didn't brake anything. 或者,当另一个开发人员处理您的代码并且没有阅读您所有出色的工作时(以防代码更长一点),他可以确保自己没有刹车。

So in your particular case I would recommend to test whether the correct error is shown, when the wrong input is given. 因此,在您的特定情况下,我建议您在输入错误时测试是否显示正确的错误。 Or what happens when a book is borrowed twice? 还是两次借书会发生什么? Stuff like that ;) 像那样的东西 ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM