java – How to use JUNIT to test void methods of my DAO class

Question:

Hello I have a DAO class and I want to implement tests with JUNIT, but many of the methods have a void return.
Here's a piece of the class that makes up the DAO package.

public class ExemplarDAO {

private Connection connection = null;

public ExemplarDAO() {
    connection = Conexao.getConexao();
}

public void Inserir(Exemplar exemplar) {
    try {
        String sql;
        sql = "INSERT INTO `exemplar`(`ID_EXE`, `ISBN`, `LiberadoParaEmprestimo`, `Duracao`, `QuantidadePaginas`, `FK_TITULO`)\n"
                + " VALUES (?,?,?,?,?,?)";
        PreparedStatement ps = connection.prepareStatement(sql);

        ps.setInt(1, exemplar.getIdExe());
        ps.setString(2, exemplar.getIsbn());
        ps.setBoolean(3, exemplar.getLiberadoParaEmprestimo());
        ps.setString(4, exemplar.getDuracao());
        ps.setString(5, exemplar.getQuantidadePaginas());
        ps.setInt(6, exemplar.getFkTitulo());

        ps.executeUpdate();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

Here the exemplar class

public class Exemplar {

private int idExe;

private int fkTitulo;

private String isbn;

private Boolean liberadoParaEmprestimo;

private String duracao;

private String quantidadePaginas;

private Titulo titulo;

public Exemplar() {
}

public Exemplar(int idExe, int fkTitulo, String isbn, Boolean liberadoParaEmprestimo, String duracao, String quantidadePaginas) {
    this.idExe = idExe;
    this.fkTitulo = fkTitulo;
    this.isbn = isbn;
    this.liberadoParaEmprestimo = liberadoParaEmprestimo;
    this.duracao = duracao;
    this.quantidadePaginas = quantidadePaginas;
}



public int getIdExe() {
    return idExe;
}

public void setIdExe(int idExe) {
    this.idExe = idExe;
}

public int getFkTitulo() {
    return fkTitulo;
}

public void setFkTitulo(int fkTitulo) {
    this.fkTitulo = fkTitulo;
}

public String getIsbn() {
    return isbn;
}

public void setIsbn(String isbn) {
    this.isbn = isbn;
}

public Boolean getLiberadoParaEmprestimo() {
    return liberadoParaEmprestimo;
}

public void setLiberadoParaEmprestimo(Boolean liberadoParaEmprestimo) {
    this.liberadoParaEmprestimo = liberadoParaEmprestimo;
}

public String getDuracao() {
    return duracao;
}

public void setDuracao(String duracao) {
    this.duracao = duracao;
}

public String getQuantidadePaginas() {
    return quantidadePaginas;
}

public void setQuantidadePaginas(String quantidadePaginas) {
    this.quantidadePaginas = quantidadePaginas;
}

public Titulo getTitulo() {
    return titulo;
}

public void setTitulo(Titulo titulo) {
    this.titulo = titulo;
}

How do I prepare JUNIT in this case?

Answer:

On test:

  • create an instance of Exemplar and pass it to the Inserir method;
  • then make a select in the table where the object should have been inserted and compare the record found in the base with the data of the Exemplar object passed by parameter.
  • Make sure the database is in a known state, for example deleting all records from the table before starting the test or rolling back the transaction at the end of the test.
  • Make sure the cradle is not being used during testing, for example using a cradle exclusively for automated testing.

What you will be testing in this case is whether Dao did the mapping correctly and whether the insert command works.

Tips:

  1. it's better to leave the connection management with the Dao consumer, rather than the Dao itself, so the test can also manage the connection by pointing to an exclusive testing base.
  2. remove this try-catch as the only thing it does for you there is make it difficult to diagnose problems.
Scroll to Top