0
私は、結果をDataTableに表示するために使用する次のコントローラスタイルを持っています。Springコントローラで単体テストを実行
@RequestMapping(value = "/list", method = RequestMethod.GET)
@ResponseBody
public DataTableObject<PatientBO> list(HttpServletRequest request,
Locale locale) {
final DataTableRequestParam param = DataTablesParamUtility
.getParam(request);
Map<Integer, String> tableColumn = new HashMap<Integer, String>();
if (tableColumn.isEmpty()) {
DataTableUtil.createMap(tableColumn, param.columnProperties);
}
return adminPatientManagementService.getList(tableColumn, param,
locale);
}
と私のDataTablesParamUtility
public class DataTablesParamUtility {
public static DataTableRequestParam getParam(HttpServletRequest request)
{
if(request.getParameter("sEcho")!=null && request.getParameter("sEcho")!= "")
{
DataTableRequestParam param = new DataTableRequestParam();
param.sEcho = request.getParameter("sEcho");
param.sSearchKeyword = request.getParameter("sSearch");
param.bRegexKeyword = Boolean.parseBoolean(request.getParameter("bRegex"));
param.iDisplayStart = Integer.parseInt(request.getParameter("iDisplayStart"));
param.iDisplayLength = Integer.parseInt(request.getParameter("iDisplayLength"));
param.iColumns = Integer.parseInt(request.getParameter("iColumns"));
param.sSearch = new String[param.iColumns];
param.bSearchable = new boolean[param.iColumns];
param.bSortable = new boolean[param.iColumns];
param.bRegex = new boolean[param.iColumns];
for(int i=0; i<param.iColumns; i++){
param.sSearch[i] = request.getParameter("sSearch_"+i);
param.bSearchable[i] = Boolean.parseBoolean(request.getParameter("bSearchable_"+i));
param.bSortable[i] = Boolean.parseBoolean(request.getParameter("bSortable_"+i));
param.bRegex[i] = Boolean.parseBoolean(request.getParameter("bRegex_"+i));
}
param.iSortingCols = Integer.parseInt(request.getParameter("iSortingCols"));
param.sSortDir = new String[param.iSortingCols];
param.iSortCol = new int[param.iSortingCols];
for(int i=0; i<param.iSortingCols; i++){
param.sSortDir[i] = request.getParameter("sSortDir_"+i);
param.iSortCol[i] = Integer.parseInt(request.getParameter("iSortCol_"+i));
}
param.searchableColumns = request.getParameter("searchableColumns");
param.columnProperties = request.getParameter("columnProperties");
param.filterBy = request.getParameter("filterBy");
param.customSearch = request.getParameter("customSearch");
param.startRange = request.getParameter("startRange");
param.endRange = request.getParameter("endRange");
return param;
}else
return null;
}
}
は今、私は私のコントローラに対するユニットテストを実行しようとしている問題が発生しています。これは、そこparam
変数がnullであるので、私はこれまで
@Test
public void testGetPatientList() throws Exception {
PatientBO p1 = new PatientBO();
p1.setId(1);
p1.setFirstName("Daenerys Targaryen");
PatientBO p2 = new PatientBO();
p2.setId(2);
p2.setFirstName("John Snow");
List<PatientBO> patientList = Arrays.asList(p1,p2);
DataTableObject<PatientBO> dto = new DataTableObject<PatientBO>();
dto.setAaData(patientList);
when(patientManagementService.getList(null, null,null)).thenReturn(dto);
mockMvc.perform(get("/staff/patient/list"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$[0].id", is(1)))
.andExpect(jsonPath("$[0].firstName", is("Daenerys Targaryen")))
.andExpect(jsonPath("$[1].id", is(2)))
.andExpect(jsonPath("$[1].firstName", is("John Snow")));
verify(patientManagementService, times(1)).getList(null, null,null);
verifyNoMoreInteractions(patientManagementService);
}
テストは失敗したものです。私の質問は
- 私のコントローラ設計ユニットはテスト可能ですか?
- 私のユニットテストはどのように書かれますか?
ですあなたの質問に詳細に答えてください。私はあなたのアプリケーションの重要な部分(エンドポイントのセットアップ:検証、リクエストの種類、パラメータ/ボディの逆シリアル化など)をテストしないで純粋な単体テストでコントローラをテストすることはありません。私の意見では、統合テストを書くべきです。詳細はこちらをご覧ください:https://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html –
なぜそうですか...春は完全にパラメータをバインドすることができますオブジェクトは 'DataTableRequestParam'に直接バインドできるように、' DataTablesParamUtility'の必要はありません。また、あなたは統合テストを書いている単体テストを書いていません!それらの静的ヘルパーメソッドへのすべての呼び出しで、テストすることは難しい(または難しい)かどうか。 –
@ M.Deinum。バインディングを 'DataTablesParamUtility'なしでどのように行うべきか私に教えてください。これは私の最初の単体テストの試みです。だから、基本的に全てが単体テストになるわけではありません。 – abiieez