Check the selected results in DBUnit

Check the selected results in DBUnit

DBUnit allows you to verify if the list of DTOs in the result of a select is correct.

You need to implement it on your own, but you can check the dto as follows.

Pass the list of DTOs of the result of the select as an argument of the assertDto method below.

public <D> void assertDto(List<D> dtoList) {
  try {
    File file = new File("dto.xlsx");
    FileInputStream fis = new FileInputStream(file);
    IDataSet dataSet = new XlsDataSet(fis);
    Itable table = dataSet.getTable("DTO");// Specify the sheet name
    Column[] cols = table.getTableMetaData().getColumns();

    // Line Number Verification
    if (table.getRowCount() != dtoList.size()) {
      fail("expected:" + String.valueOf(table.getRowCount)) + "\r\nactual:" + String.valueOf(dtoList.size()) + "\r\n");
    }

    for (int row = 0;row < table.getRowCount(); row++) {
      D dto = dtoList.get(row);
      for (Column col : cols) {
        Object expected = table.getValue(row, col.getColumnName());
        Object actual = Deencapsulation.getField(dto, col.getColumnName());

        assertThat(actual, is(expected));
        if (!Objects.equals(actual, expected)) {
          fail(Integer.parseInt(row + 1) + ":" + col.getColumnName() + "\r\n");
        }
      }
    }
  } catch (AssertionError e) {
    throw e;
  } catch (Exception e) {
    e.printStackTrace();
    throw new RuntimeException("DTOエラー");
  }
}

Describe the contents of dto, the extracted result of select, in dto.xlsx.

The first line describes the property name, and the second and subsequent lines describe the selected result.

Incidentally, the table.getRowCount() method of Itable can be used to obtain the number of rows excluding header rows.

コメント

タイトルとURLをコピーしました