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.
– Caffé
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.
– Caffé
Thank you and this is what I wanted
– Kai
@Caffé why not publish an answer to that question?
– Fagner Fonseca