2017-03-01 85 views
0

我使用以下控制器樣式將結果顯示到DataTable。在彈簧控制器上執行單元測試

@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; 
    } 
} 

現在我有問題試圖運行對我的控制器單元測試。這是我迄今爲止

@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); 
    } 

測試失敗,因爲param變量爲空。我的問題是

  1. 我的控制器設計單元是否可測試?
  2. 我應該如何寫單元測試?
+0

你已經發布的代碼是不夠的,能夠以詳細的方式回答你的問題。我從來沒有在純粹的單元測試中測試控制器,因爲您跳過應用程序的重要部分(端點設置:驗證,請求類型,參數/正文反序列化等)未經測試。我認爲你應該嘗試寫一個集成測試。更多可以在這裏找到:https://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html –

+1

爲什麼哦爲什麼...春天是完全能夠綁定參數對象,因此不需要DataTablesParamUtility,因爲您應該可以直接綁定到DataTableRequestParam。你還沒有寫一個單元測試,你正在編寫一個集成測試!所有對這些靜態幫助器方法的調用都難以測試(或困難)。 –

+0

@ M.Deinum。你能告訴我如何在沒有'DataTablesParamUtility'的情況下完成綁定嗎?這是我第一次嘗試單元測試,所以基本上不是所有的東西都可以通過單元測試。 – abiieez

回答

1

您可以測試像下面您的端點,如果您使用的春天SpringBootTest

@RunWith(SpringRunner.class) 
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = MyApplication.class) 
public class DataTableObjectTest { 
    @Autowired 
    private TestRestTemplate restTemplate; 

    @Test 
    pubic void testDataList() { 

     DataTableObject<PatientBO> dto = (DataTableObject<PatientBO>) restTemplate.exchange("/list", HttpMethod.GET, request, DataTableObject.class); 

     // perform the asserts of dto 
    } 
} 

注:爲MyApplication類是你的春天開機啓動類