我有以下控制器样式,用于向DataTable显示结果。
settings.gradle
和我的DataTablesParamUtility
@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);
}
现在我在尝试对我的控制器运行单元测试时遇到问题。这就是我到目前为止所拥有的
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;
}
}
测试失败,因为@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);
}
变量为null。我的问题是
答案 0 :(得分:1)
如果使用spring SpringBootTest,可以像下面一样测试端点:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = MyApplication.class)
public class DataTableObjectTest {
@Autowired
private TestRestTemplate restTemplate;
@Test
public void testDataList() {
DataTableObject<PatientBO> dto
= (DataTableObject<PatientBO>) restTemplate.exchange("/list",
HttpMethod.GET,
request,
DataTableObject.class);
// perform the asserts of dto
}
}
注意:MyApplication类是您的spring boot入门类。