0

I have some methods that delete, update and put some data to the database (MySQL). I can check by myself that the data was added, deleted, and updated but I want some tests on my project.

How can I do it?

I use the code below to get the first and last names for the database using the ID (Primary_Key).

public void checkId() {
    int idNumber = 0;
    try {
        idNumber = Integer.parseInt(textFieldForId.getText());
    } catch (NumberFormatException ex) {
        messageLabel.setText("PLEASE ENTER A NUMERIC VALUE");
    }
    try {
        Connection con = DataBaseConnecction.getDataBaseConnection();
        Statement statement = con.createStatement();
        String sql = "select firstName , lastName from patients_info where id = " + idNumber;
        ResultSet result = statement.executeQuery(sql);
        while (result.next())
            patientFullName.setText(result.getString("firstName") + " " + result.getString("lastName"));
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (patientFullName.getText().isEmpty() && messageLabel.getText().isEmpty())
        messageLabel.setText("THERE IS NO PATIENT WITH THIS ID NUMBER");
}

void deletePatient() {
    int idNumber;
    idNumber = Integer.parseInt(textfieldforCin.getText());
    try {
        Connection con = DataBaseConnecction.getDataBaseConnection();
        String sql = "delete from patients_info where id = " + "'" + idNumber + "'";
        int roweffected = con.createStatement().executeUpdate(sql);
        messagelabel.setText("DELETED SUCCESS ");
    } catch (SQLException ex) {
        ex.printStackTrace();
    } catch (NumberFormatException E) {
        messagelabel.setText("SET A CORRECT CIN NUMBER ");
    }
}
Abra
  • 19,142
  • 7
  • 29
  • 41
  • 1
    Are you asking more generally about [How to do database unit testing](https://stackoverflow.com/questions/3772093/how-to-do-database-unit-testing)? – andrewJames Apr 28 '22 at 14:20
  • 1
    removed javafx tag, looks unrelated - is it? If so, explain why exactly it's a fx problem, best with a [mcve] demonstrating what you tried and how it didn't work. – kleopatra Apr 28 '22 at 14:26

1 Answers1

-1

I think you are talking about an integration test here - if so then your test should 1) Add a record 2) Retrieve the same record 3) Assert that both are the same

Michael
  • 152
  • 1
  • 4