DBUnitでセレクトされた結果を確認する
DBUnitは使い方がまだいまいちわかってませんが、selectした結果のDTOのListが正しいかどうかも検証可能です。
独自で実装する必要がありますが以下のようにすればdtoの確認は可能です。
以下のassertDtoメソッドの引数にselectした結果のDTOのListを渡します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
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");// シート名を指定します Column[] cols = table.getTableMetaData().getColumns(); // 行数検証 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エラー"); } } |
dto.xlsxにselectの抽出結果のdtoの内容を記述します。
1行目はプロパティ名を記述し、2行目以降selectされた結果を記述します。
ちなみにItableのtable.getRowCount()メソッドで、ヘッダ行を除いた行数を取得することができます。
KHI入社して退社。今はCONFRAGEで正社員です。関西で140-170/80~120万から受け付けております^^
得意技はJS(ES6),Java,AWSの大体のリソースです
コメントはやさしくお願いいたします^^
座右の銘は、「狭き門より入れ」「願わくは、我に七難八苦を与えたまえ」です^^