【Junit】excelのデータを読みつつJunitテストを実行する
package viewda;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.List;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.junit.experimental.theories.DataPoints;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
@RunWith(Theories.class)
public class testFlowIns {
@DataPoints
//1,2,3..param 4...return
public static Fixture[] getParams() throws EncryptedDocumentException, IOException{
List<Fixture> lst = new ArrayList<Fixture>();
Workbook excel = WorkbookFactory.create(new File("read_test_account.xls"));
Sheet sheet = excel.getSheet("Sheet1");
int rowStart = sheet.getFirstRowNum();
int rowEnd = sheet.getLastRowNum();
int lastColumn = 0;
for (int i = rowStart; i <= rowEnd; i++) {
Row row = sheet.getRow(i);
if (row == null) {
System.out.println(",,,,,");
continue;
}
else if (i == 0) {
lastColumn = row.getLastCellNum();
}
if(i>0) {
lst.add(new Fixture(row));
}
}
return lst.toArray(new Fixture[0]);
};
static class Fixture {
String id;
String account_code;
String ln_type;
BigDecimal flow_amount;
Fixture(Row row) {
this.id= row.getCell(0).getStringCellValue();
this.account_code= row.getCell(1).getStringCellValue();
this.ln_type= row.getCell(2).getStringCellValue();
this.flow_amount = BigDecimal.valueOf(row.getCell(3).getNumericCellValue());
}
}
@Theory
public void testInsFlowaccount(Fixture params) throws Exception {
insFlowAccount objP = new insFlowAccount();
Method method =
objP.getClass().getDeclaredMethod("insFlowAccountData",
String.class,String.class,String.class,BigDecimal.class);
method.setAccessible(true);
int i=
(int)method.invoke(objP, params.id,params.account_code,params.ln_type,params.flow_amount);
}
}