mercredi 1 juin 2016

how to test crud operations in java

Please explain how I can test CRUD operations in java like these:

public void insert(User user) {
    Connection con = null;
    PreparedStatement ps;
    try {
        con = DatabaseConnection.dbCon();
        ps = con.prepareStatement("insert into usert (name,surname,role_id,login,password) values(?,?,?,?,?)");
        ps.setString(1, user.getName());
        ps.setString(2, user.getSurname());
        ps.setInt(3, user.getRole().getId());
        ps.setString(4, user.getLogin());
        ps.setString(5, user.getPassword());
        ps.execute();
        if (con != null) {
            con.close();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

/**
 * deletes user
 * @param user
 */
public void delete(User user) {
    Connection con = null;
    PreparedStatement ps;
    try {
        con = DatabaseConnection.dbCon();
        ps = con.prepareStatement("delete from usert where id = ?");
        ps.setInt(1, user.getId());
        ps.execute();
        if (con != null) {
            con.close();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

Now, I wrote unit tests for models like User and etc. I don't understand how to test buttons and another operations like above.

Aucun commentaire:

Enregistrer un commentaire