lundi 5 novembre 2018

How to handle parameters in testing rest api?

I am currently testing my REST API with jUnit. I am not familiar with testing or jUnit. So I am not sure this is right way to test. Anyway here's my testing code about HTTP Request.

public void testGetMethod_array(String url, String[] paramName, String[] paramValue) throws Exception {
    mockMvc.perform(get(url).param(paramName[0], paramValue[0]).param(paramName[1], paramValue[1]).param(paramName[2], paramValue[2]))
           .andDo(print())
           .andExpect(status().isOk());
}

With this method, I try to test each endpoint like this.

@Test
public void employee_list() throws Exception {
    String[] list_paramName = { "locale", "empKey", "searchWord", "startPage", "pageCount" };
    String[] list_paramValue = { locale, empKey, searchWord, startPage, pageCount };
    testGetMethod_array("/employee/list", list_paramName, list_paramValue);
}

@Test
public void employee_image() throws Exception {
    String[] image_paramName = { "locale", "empKey", "height" };
    String[] image_paramValue = { locale, empKey, height };
    testGetMethod_array("/employee/image", image_paramName, image_paramValue);
}

@Test
public void employee_detail() throws Exception {
    String[] detail_paramName = { "locale", "empKey", "requestEmpKey" };
    String[] detail_paramValue = { locale, empKey, requestEmpKey };
    testGetMethod_array("/employee/detail", detail_paramName, detail_paramValue);
}

@Test
public void employee_web() throws Exception {
    String[] web_paramName = { "locale", "empKey", "targetEmpKey" };
    String[] web_paramValue = { locale, empKey, targetEmpKey };
    testGetMethod_array("/employee/web", web_paramName, web_paramValue);
}

And my problem is each endpoint needs their own parameters. The number of parameters and the value of parameters are all different. So I tried to modify my code like this to use FOR or IF statement.

public void testGetMethod_param(String url, String locale, String empKey, String accessToken) throws Exception {
    MultiValueMap<String, String> paraMap =new LinkedMultiValueMap<>();
    paraMap.add("locale", locale);
    paraMap.add("empKey", empKey);
    paraMap.add("accessToken", accessToken);
    mockMvc.perform(get(url).params(paraMap))
    .andDo(print())
    .andExpect(status().isOk());
}

But To use "params", I need Spring Framework Version above 4.2.4. And I can't upgrade the Framework Version right now. Is there any way to handle this problem? I greatly appreiciate your help.

Aucun commentaire:

Enregistrer un commentaire