How to test with Junit in a C R U D?

Asked

Viewed 552 times

0

I would like to know the condition to use within the test.

Got the class Customer

package br.myhome.web.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Cliente {

private int id;
private String nome;
private String telefone;
private String email;

public Cliente() {}


public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getNome() {
    return nome;
}
public void setNome(String nome) {
    this.nome = nome;
}
public String getTelefone() {
    return telefone;
}
public void setTelefone(String telefone) {
    this.telefone = telefone;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}


@Override
public String toString() {
    return "Cliente [id=" + id + ", nome=" + nome + ", telefone=" + telefone + ", email=" + email + "]";
}

// C R U D

public void cadastrar(Connection conn) {
    String sqlInserir = 
            "INSERT INTO CLIENTE (nome, telefone, email) VALUES (?, ?, ?)";
    PreparedStatement stm = null;
    try {
        stm = conn.prepareStatement(sqlInserir);
        stm.setString(1, getNome());
        stm.setString(2, getTelefone());
        stm.setString(3, getEmail());
        stm.execute();
    } catch (Exception e) {
        e.printStackTrace();
        try {
            conn.rollback();
        } catch (SQLException e1) {
            System.out.println(e1.getStackTrace());
        }
    } finally {
        if (stm!=null) {
            try {
                stm.close();
            } catch (SQLException e1) {
                System.out.println(e1.getStackTrace());

            } 
        }
    }
}

public void alterar (Connection conn) {
    String sqlAlterar = 
            "UPDATE CLIENTE  SET nome = ?, telefone = ?, email = ? WHERE ID = ?";
    PreparedStatement stm = null;
    try {
        stm = conn.prepareStatement(sqlAlterar);
        stm.setString(1, getNome());
        stm.setString(2, getTelefone());
        stm.setString(3, getEmail());
        stm.executeUpdate();
    } catch (Exception e1) {
        e1.printStackTrace();
    } try {
        conn.rollback();
    } catch (SQLException e1) {
        System.out.println(e1.getStackTrace());

    } finally {
        if (stm != null) {
            try {
                stm.close();
            } catch (SQLException e1) {
                System.out.println(e1.getStackTrace());
            }
        }
    }
}

public void excluir(Connection conn) {
    String sqlExcluir = 
            "DELETE FROM CLIENTE WHERE ID = ?";
    PreparedStatement stm = null;
    try {
        stm = conn.prepareStatement(sqlExcluir);
        stm.setInt(1, getId());
        stm.execute();
    } catch (SQLException e) {
        e.printStackTrace();
        try {
            conn.rollback();
        } catch (SQLException e1) {
            System.out.println(e1.getStackTrace());
        } 
    }   finally {
        if (stm!= null) {
            try {
                stm.close();
            } catch (SQLException e1) {
                System.out.println(e1.getStackTrace());
            }
        }
    }
}

public void carregar (Connection conn) {
    String sqlCarregar = 
            "SELECT id, nome, telefone, email WHERE ID = ?";
    PreparedStatement stm = null;
    ResultSet rs = null;
    try {
        stm = conn.prepareStatement(sqlCarregar);
        stm.setInt(1, getId());
        stm.executeQuery();

        if (rs.next()){
            this.setNome(rs.getString(1));
            this.setTelefone(rs.getString(2));
            this.setEmail(rs.getString(3));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } try {
        conn.rollback();
    } catch (SQLException e1) {
        System.out.println(e1.getStackTrace());
    } finally {
        if (rs != null) {
            try {rs.close();
            } catch (SQLException e1) {
                System.out.println(e1.getStackTrace());
            }
        } if (stm != null) {
            try {
                stm.close();
            } catch (SQLException e1) {
                System.out.println(e1.getStackTrace());
            }
        }
    }
}

And the test class

@Test
void testCadastrar() {
if (c.cadastrar(b.conector())) // preciso de ajuda nessa linha, creio eu
System.out.println(“Sucesso”);
else
System.out.println(“falha”);

}
  • You can use Mocks to simulate persistence, and to check the result, you will need to use Junit Assertions because it validates the final result of your method. In case you decide to test what was persisted, before running the test, enter the records in the database and test if they exist through Assertions.

  • A very primitive test that you can do with your code the way it is would be to execute your registration method, for example, let it enter in the bank and then do a search to see if it entered. The big problem with their methods is that they don’t return anything. Ideally you return a boolean or even the object you saved or loaded from the bank. This way you can test whether the return is null or not, whether it is true or false etc. Try to fetch material on unit tests. Theory is important before practice, so you know what to test.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.