Hi everyone i'm new here and i need your help to unit test with junit a DAO class with a connection pool. This is the ConPool:
public class ConPool {
private static DataSource datasource;
/**
* {@return} Connection
* {@throws} SQLException
* Ritorna la connessione al db.
*/
public static Connection getConnection() throws SQLException {
if (datasource == null) {
PoolProperties p = new PoolProperties();
p.setUrl("jdbc:mysql://localhost:3306/GameLand?serverTimezone="
+ TimeZone.getDefault().getID());
p.setDriverClassName("com.mysql.cj.jdbc.Driver");
p.setUsername("root");
p.setPassword("basedidati");
p.setMaxActive(100);
p.setInitialSize(10);
p.setMinIdle(10);
p.setRemoveAbandonedTimeout(60);
p.setRemoveAbandoned(true);
datasource = new DataSource();
datasource.setPoolProperties(p);
}
return datasource.getConnection();
}
}
and this is the DAO class that i want to test:
public class OrdineDAO {
/**
* {@return} ArrayList of Ordine.
*/
public synchronized ArrayList<Ordine> doRetrieveAll() {
String query = "SELECT * FROM ordine";
ArrayList<Ordine> result = new ArrayList<Ordine>();
try (Connection conn = ConPool.getConnection()) {
PreparedStatement ps = conn.prepareStatement(query);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Ordine ord = new Ordine();
ord.setConsegnato(rs.getBoolean("consegnato"));
ord.setDataOra(rs.getString("dataOra"));
ord.setIdOrdine(rs.getInt("idOrdine"));
ord.setIdProdotto(rs.getInt("idProdotto"));
ord.setPrezzoFis(rs.getDouble("prezzoFis"));
ord.setPrezzoDig(rs.getDouble("prezzoDig"));
ord.setIva(rs.getDouble("iva"));
ord.setQuantitaDigitale(rs.getInt("quantitaDigitale"));
ord.setQuantitaFisico(rs.getInt("quantitaFisico"));
ord.setIdUtente(rs.getInt("idUtente"));
result.add(ord);
}
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
/**
* {@param} id: int.
* {@return} ArrayList of Ordine.
*/
public synchronized ArrayList<Ordine> doRetrieveByUser(int id) {
PreparedStatement ps = null;
String query = "SELECT * FROM ordine WHERE idUtente = ?";
ArrayList<Ordine> result = new ArrayList<Ordine>();
try (Connection conn = ConPool.getConnection()) {
ps = conn.prepareStatement(query);
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Ordine ord = new Ordine();
ord.setConsegnato(rs.getBoolean("consegnato"));
ord.setDataOra(rs.getString("dataOra"));
ord.setIdOrdine(rs.getInt("idOrdine"));
ord.setIdProdotto(rs.getInt("idProdotto"));
ord.setPrezzoFis(rs.getDouble("prezzoFis"));
ord.setPrezzoDig(rs.getDouble("prezzoDig"));
ord.setIva(rs.getDouble("iva"));
ord.setQuantitaDigitale(rs.getInt("quantitaDigitale"));
ord.setQuantitaFisico(rs.getInt("quantitaFisico"));
ord.setIdUtente(rs.getInt("idUtente"));
result.add(ord);
}
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
/**
* {@param} data1: String.
* {@param} data2: String.
* {@return} ArrayList of Ordine.
*/
public synchronized ArrayList<Ordine> doRetrieveByDate(String data1, String data2) {
PreparedStatement ps = null;
String query = "SELECT * FROM ordine WHERE dataOra >= ? AND dataOra <= ?";
ArrayList<Ordine> result = new ArrayList<Ordine>();
try (Connection conn = ConPool.getConnection()) {
ps = conn.prepareStatement(query);
ps.setString(1, data1);
ps.setString(2, data2);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Ordine ord = new Ordine();
ord.setConsegnato(rs.getBoolean("consegnato"));
ord.setDataOra(rs.getString("dataOra"));
ord.setIdOrdine(rs.getInt("idOrdine"));
ord.setIdProdotto(rs.getInt("idProdotto"));
ord.setPrezzoFis(rs.getDouble("prezzoFis"));
ord.setPrezzoDig(rs.getDouble("prezzoDig"));
ord.setIva(rs.getDouble("iva"));
ord.setQuantitaDigitale(rs.getInt("quantitaDigitale"));
ord.setQuantitaFisico(rs.getInt("quantitaFisico"));
ord.setIdUtente(rs.getInt("idUtente"));
result.add(ord);
}
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
}
starting first with doRetrieveAll method, i tried to do a
@Test
public void doRetrieveAll_Success() throws SQLException {
assertNotNull(ordineDAO.doRetrieveAll());
}
But i need to know how to set the @BeforeAll in order to test this method. Can you please help me understanding how to set properly the test class? Thank you
Aucun commentaire:
Enregistrer un commentaire