1
2

More than 3 years have passed since last update.

Excelファイルの内容をJUnitでテスト

Posted at

POIライブラリを用いてHttpServletResponseに出力したExcelファイルの内容をJUnitでテストする方法。
responseからの読込にハマったので備忘として残します。

testクラスではMockHttpServletResponseを用いてresponseを生成する。

//テスト対象クラスへ渡すresponse
MockHttpServletResponse response = new MockHttpServletResponse();

//テスト対象クラスで処理したresponseから出力するExcelファイルをInputStreamで読込
InputStream inputStream = new ByteArrayInputStream(response.getContentAsByteArray());

xlsx形式はバイナリファイルなので、
HttpServletResponsegetContentAsByteArray()を用いてInputStreamで読み込む事ができる。

後は

Workbook workbook = WorkbookFactory.create(inputStream);

でExcelファイルを開いて内容を1セルずつtestしていけばOK。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2