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

Asked

Viewed 782 times

3

Hello I have a DAO class and I want to implement tests with JUNIT, but many of the methods have the return void.
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 I prepare JUNIT in this case?

  • In the test: create an instance of Exemplary and move on to the method Insert; then select in the table where the object should have been inserted and compare the record found in the database 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 rollback the transaction at the end of the test. Make sure the base is not being used during testing, for example using a unique basis for automated testing.

  • What you will be testing, in this case, is whether Dao did the right mapping and whether the Insert command works. Hints: to) it is better to leave the management of the connection with the Dao consumer, instead of leaving in Dao itself, so the test can also manage the connection pointing to a unique test base. b) remove this Try-catch because the only thing he does for you out there is make it difficult to diagnose problems.

  • Thank you and this is what I wanted

  • @Caffé why not publish an answer to that question?

2 answers

0

On the test:

  • create an instance of Example and pass to the method Inserir;
  • then select the table where the object should have been inserted and compare the record found in the database with the object data Exemplar 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 doing rollback transaction at the end of the test.
  • Make sure the base is not being used during testing, for example using a unique basis for automated testing.

What you will be testing, in this case, is whether Dao did the correct mapping and whether the Insert command works.

Tips:

  1. it is better to leave the management of the connection with the Dao consumer, in instead of leaving in the Dao in itself, so the test can also manage the connection pointing to a unique test base.
  2. remove this try-catch because the only thing he does for you out there is to make it difficult to diagnose problems.

0

One should be careful with Daos tests. Unit tests should test logic. So it should have a controlled environment. The inputs and outputs of these methods must be repeatable, retentable, reliable. When these entries or results may change by external factors (e.g., the data in the database changes) your test is not considered reliable. Your test runs the risk of needing to be refactored frequently.

Ideally, the DAO service that would go in the base (exemplarDAO.insert) would be mocked. If you still want to test in this case, the ideal is that your 'Connection' is mocked and is used 'when' junit to simulate behaviour.

Another method that can also be used as a void method is the 'Verify', where you control the points where the processing went.

Here are two topics in stackoverflow on this subject:

Browser other questions tagged

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