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

3

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 exemplary 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?

    
asked by anonymous 22.11.2016 / 16:02

1 answer

0

On test:

  • create an instance of Copy and go to method Inserir ;
  • 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 object Exemplar passed by parameter.
  • Ensure that the database is in a known state, for example by deleting all the records in the table before starting the test or by doing rollback of the transaction at the end of the test.
  • Ensure that the database is not being used during the tests, for example using a database for automated testing.

What you will be testing in this case is whether Dao has done the correct mapping and if the insert command works.

Tips:

  • It is better to leave the management of the Dao consumer connection in instead of leaving it in Dao itself, so the test can also manage the pointing to an exclusive testing base.
  • remove this try-catch because the only thing he does for you there is making it difficult to diagnose problems.
  • 27.09.2018 / 06:36