I have a method that contains a switch case block which iterates through an excel file to obtain values to write to a db. I need to write a unit test for this method but I have no idea where to start.
Below is the method of which I need to write the unit test for. If anyone could advise me it would be greatly appreciated.
public void loadAccount() throws Exception {
Connection conn = null;
PreparedStatement insert = null;
Properties props = new Properties();
try {
conn = connMan.allocateConnection();
conn.setAutoCommit(false);
insert = conn.prepareStatement(INSERT_SQL_TOP_ACCOUNTS);
FileInputStream inputStream = new FileInputStream("path");
props.load(inputStream);
String excelFilePath = props.getProperty("Account");
FileInputStream iStream = new FileInputStream(new File(excelFilePath));
Workbook workbook = new XSSFWorkbook(iStream);
Sheet firstSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = firstSheet.iterator();
// Current date for entry into db
java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis());
insert.setDate(1, sqlDate);
// Skips the header row
iterator.next();
// Iterate through the first sheet and get the cell values
while (iterator.hasNext()) {
Row nextRow = iterator.next();
Iterator<Cell> cellIterator = nextRow.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
int columnIndex = cell.getColumnIndex();
switch (columnIndex) {
case 0:
String institution = cell.getStringCellValue();
insert.setString(2, institution);
break;
case 1:
// Formatting data because of alphanumeric mix in row
DataFormatter formatter = new DataFormatter();
String dept = formatter.formatCellValue(cell);
insert.setString(3, dept);
break;
case 2:
int hits = (int) cell.getNumericCellValue();
insert.setInt(4, hits);
break;
case 3:
int visitors = (int) cell.getNumericCellValue();
insert.setInt(5, visitors);
break;
}
}
insert.addBatch();
}
int[] insertCount = insert.executeBatch();
int successInserted = 0;
for (int item : insertCount) {
if (item == 1) {
successInserted++;
}
}
log.info("There're " + insertCount.length + " need to be inserted, with successfully [" + successInserted
+ "] inserted");
conn.commit();
} catch (Exception e) {
log.error("Exception in loadAccount:" + e);
try {
conn.rollback();
} catch (SQLException e1) {
log.error("Exception when rollback the loading.", e1);
throw e1;
}
throw e;
} finally {
connMan.closeStatement(insert);
try {
if (conn != null) {
connMan.deallocateConnection(conn);
}
} catch (SQLException e) {
log.error("Exception in loadAccount:" + e);
throw e;
}
}
}
Aucun commentaire:
Enregistrer un commentaire