Reputation: 109
I have to add tests for this java program. 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);
}
}
}
Upvotes: 0
Views: 127
Reputation: 546
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 ;)
Upvotes: 1
Reputation: 57184
You are going to have a very hard time adding tests here since your method does everything:
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.
Upvotes: 5