Guest
Java Best Practice Unit Tests mit Junit
Post
by Guest » 11 Feb 2025, 04:40
Ich verwende derzeit die folgende Methode und den Unit -Test für die Methode. Ich denke usw. ... Alle Hilfe wird geschätzt.
Code: Select all
public static ArrayList executeSelect(
Connection conn, Statement stmt, Query query) {
ResultSet rs = null;
ArrayList serviceRequests = new ArrayList();
try {
long queryStart = System.nanoTime();
rs = stmt.executeQuery(query.getQuery());
long queryEnd = System.nanoTime();
long queryDuration = queryEnd-queryStart;
queryTime = String.valueOf(queryDuration);
while (rs.next()) {
HashMap serviceRequestData = new HashMap();
if (QueryUtil.hasColumn(rs, "ID")) {
String id = rs.getString("ID");
serviceRequestData.put("ID", id);
}
else{
serviceRequestData.put("ID", " ");
}
if (QueryUtil.hasColumn(rs, "FN_Contact")) {
String firstName = rs.getString("FN_Contact");
serviceRequestData.put("FN_Contact", firstName);
}
else{
serviceRequestData.put("FN_Contact", " ");
}
if (QueryUtil.hasColumn(rs, "LN_Contact")) {
String lastName = rs.getString("LN_Contact");
serviceRequestData.put("LN_Contact", lastName);
}
else{
serviceRequestData.put("LN_Contact", " ");
}
if (QueryUtil.hasColumn(rs, "Notes")) {
String notes = rs.getString("Notes");
serviceRequestData.put("Notes", notes);
}
else{
serviceRequestData.put("Notes", " ");
}
if (QueryUtil.hasColumn(rs, "Email")) {
String email = rs.getString("Email");
serviceRequestData.put("Email", email);
}
else{
serviceRequestData.put("Email", " ");
}
serviceRequests.add(serviceRequestData);
}
} catch (SQLException e) {
e.printStackTrace();
sqlException = true;
}
return serviceRequests;
}
< /code>
JUNIT -Test: < /p>
@Test
public void testFirstName() {
ArrayList testMap = new ArrayList();
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String connectionUrl = "jdbc:mysql://localhost:3306/gc_image";
String connectionUser = "root";
String connectionPassword = "GCImage";
conn = DriverManager.getConnection(connectionUrl, connectionUser,
connectionPassword);
conn.
stmt = conn.createStatement();
Query testQuery = new Query();
testQuery
.setQuery("select * from service_request where FN_contact = 'Trevor'");
testMap = QueryController.executeSelect(conn, stmt, testQuery);
assertEquals("Janke", testMap.get(0).get("LN_Contact"));
assertEquals("Hello World", testMap.get(0).get("Notes"));
assertEquals("janke@gmail.com", testMap.get(0).get("Email"));
assertEquals("ID", testMap.get(0).get("7"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
1739245229
Guest
Ich verwende derzeit die folgende Methode und den Unit -Test für die Methode. Ich denke usw. ... Alle Hilfe wird geschätzt.[code]public static ArrayList executeSelect( Connection conn, Statement stmt, Query query) { ResultSet rs = null; ArrayList serviceRequests = new ArrayList(); try { long queryStart = System.nanoTime(); rs = stmt.executeQuery(query.getQuery()); long queryEnd = System.nanoTime(); long queryDuration = queryEnd-queryStart; queryTime = String.valueOf(queryDuration); while (rs.next()) { HashMap serviceRequestData = new HashMap(); if (QueryUtil.hasColumn(rs, "ID")) { String id = rs.getString("ID"); serviceRequestData.put("ID", id); } else{ serviceRequestData.put("ID", " "); } if (QueryUtil.hasColumn(rs, "FN_Contact")) { String firstName = rs.getString("FN_Contact"); serviceRequestData.put("FN_Contact", firstName); } else{ serviceRequestData.put("FN_Contact", " "); } if (QueryUtil.hasColumn(rs, "LN_Contact")) { String lastName = rs.getString("LN_Contact"); serviceRequestData.put("LN_Contact", lastName); } else{ serviceRequestData.put("LN_Contact", " "); } if (QueryUtil.hasColumn(rs, "Notes")) { String notes = rs.getString("Notes"); serviceRequestData.put("Notes", notes); } else{ serviceRequestData.put("Notes", " "); } if (QueryUtil.hasColumn(rs, "Email")) { String email = rs.getString("Email"); serviceRequestData.put("Email", email); } else{ serviceRequestData.put("Email", " "); } serviceRequests.add(serviceRequestData); } } catch (SQLException e) { e.printStackTrace(); sqlException = true; } return serviceRequests; } < /code> JUNIT -Test: < /p> @Test public void testFirstName() { ArrayList testMap = new ArrayList(); Connection conn = null; Statement stmt = null; try { Class.forName("com.mysql.jdbc.Driver"); String connectionUrl = "jdbc:mysql://localhost:3306/gc_image"; String connectionUser = "root"; String connectionPassword = "GCImage"; conn = DriverManager.getConnection(connectionUrl, connectionUser, connectionPassword); conn. stmt = conn.createStatement(); Query testQuery = new Query(); testQuery .setQuery("select * from service_request where FN_contact = 'Trevor'"); testMap = QueryController.executeSelect(conn, stmt, testQuery); assertEquals("Janke", testMap.get(0).get("LN_Contact")); assertEquals("Hello World", testMap.get(0).get("Notes")); assertEquals("janke@gmail.com", testMap.get(0).get("Email")); assertEquals("ID", testMap.get(0).get("7")); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } [/code]