jeudi 9 juillet 2020

JUNIT testing Apache POI XSSF Workbook

I have to write a junit test for existing java programs which generates an Apache POI XSSF Workbook. And I have no idea how to write a junit test for methods which you can see. Can someone help me please. This is WBgenerator.

public void generateWorkbook() throws IOException {

    RowsAndCellsCourse rowsAndCellsCourse = new RowsAndCellsCourse();
    RowsAndCellsAttendance rowsAndCellsAttendance = new RowsAndCellsAttendance();

    Workbook workbook = new XSSFWorkbook();
    CreationHelper createHelper = workbook.getCreationHelper();
    Sheet sheetAttendance = workbook.createSheet("Course");
    Sheet sheetCourse = workbook.createSheet("Lesson");

    //Adding borders
    CellStyle style = workbook.createCellStyle();


    //Generate rows on 1 sheet, participants info
    rowsAndCellsAttendance.generateFirstRow(sheetAttendance, style);
    rowsAndCellsAttendance.generateSecondRow(sheetAttendance, style);

    //Generate rows on 2 sheet, lesson info
    rowsAndCellsCourse.generateFirstRowSheet2(sheetCourse, style);
    rowsAndCellsCourse.generateRowsSheet2(sheetCourse, style);

    for (Sheet sheet : workbook) {
        for (Row row : sheet) {
            for (Cell cell : row) {
                style.setBorderBottom(BorderStyle.THIN);
                style.setBottomBorderColor(IndexedColors.BLACK.getIndex());
                style.setBorderLeft(BorderStyle.THIN);
                style.setLeftBorderColor(IndexedColors.BLACK.getIndex());
                style.setBorderRight(BorderStyle.THIN);
                style.setRightBorderColor(IndexedColors.BLACK.getIndex());
                style.setBorderTop(BorderStyle.THIN);
                style.setTopBorderColor(IndexedColors.BLACK.getIndex());
                cell.setCellStyle(style);
            }
        }
    }
    //Saving
    saveWorkbook(workbook);


}

public void saveWorkbook(Workbook workbook) throws IOException {
    try (OutputStream fileOut = new FileOutputStream("workbook.xlsx")) {
        workbook.write(fileOut);
    }
}

} Here you can see programs which generates workbook where are methods which generates rows and cell. So I tried to write junit tests for both methods but it doesn't work.

And this is Rows and Cells generator

public class RowsAndCellsAttendance {

Participant participant = new Participant();
public void generateFirstRow(Sheet sheet, CellStyle style) {


    Row row = sheet.createRow(0);

    row.createCell(0).setCellValue("Name");

    row.createCell(1).setCellValue("Mobile phone");

    row.createCell(2).setCellValue("E-mail");


}

public void generateSecondRow(Sheet sheet, CellStyle style) {

    for (Integer rowNum = 1; rowNum < 3; rowNum++) {

            Row row2 = sheet.createRow(rowNum);


            row2.createCell(0).
                    setCellValue(participant.setName("") + ", "
                            + participant.setSurname(""));

            row2.createCell(1).
                    setCellValue(participant.setMobile(""));

            row2.createCell(2).
                    setCellValue(participant.setEmail(""));

        }
    }
}

P.S. I new in java if you will see any mistakes please review .

Aucun commentaire:

Enregistrer un commentaire