簡體   English   中英

如何編寫用於返回Java中的列表的靜態服務的測試用例

[英]How to write the test cases that for an restful service that return a list in java

我正在嘗試使用Junit Mockito為服務編寫一個測試案例。

控制器類:

public class ReporteesService {
   ReporteeList   reportee   = new ReporteeList();
       GetEmpId       EmpId      = new GetEmpId();
       public ReporteesService(ReporteeList reportee) {
       this.reportee = reportee;
      }
       public ReporteesService(EmpJiraList NumberOfJiras) {
       this.NumberOfJiras = NumberOfJiras;
      }
       public ReporteesService(GetEmpId EmpId) {
       this.EmpId = EmpId;
      }
   @GET
   @Path("/ReporteeList/{empid}")
   @Produces(MediaType.APPLICATION_JSON)
       public List<Map<Object, Object>> getList(@PathParam("empid")String 
       emp ) throws Exception {
   String id       = EmpId.getEmpId(emp);
       int employeeid  = Integer.parseInt(id);
       return  reportee.getReportees(employeeid);
   }

ReporteeList類:

       public class ReporteeList {
   public List<Map<Object,Object>> getReportees(int idOfEmp) throws 
       Exception {
    Authentication auth = new Authentication();
    JiraCount      count = new JiraCount();
    String api = "https://*******/core/v3/people/";
    int id = idOfEmp;
    String ext = "/@reports";
    String url = api + id + ext;
    String authToken = auth.getToken();
    Client restClient = Client.create();
    WebResource webResource = restClient.resource(url);
    ClientResponse resp = 
            webResource.accept("application/json").header("Authorization", 
            "Basic " + authToken)
            .get(ClientResponse.class);
    if (resp.getStatus() != 200) {
        System.err.println("Unable to connect to the server");
    }
    String output = resp.getEntity(String.class);

    // JSONParser reads the data from string object and break each 
            data into key
    // value pairs
    JSONParser parse = new JSONParser();
    // Type caste the parsed json data in json object
    JSONObject jobj = (JSONObject) parse.parse(output);
    // Store the JSON object in JSON array as objects (For level 1 
            array element i.e list)

    JSONArray jsonarr_s = (JSONArray) jobj.get("list");
    List<Map<Object, Object>> List = new 
             ArrayList<Map<Object,Object>>();

    // Get data for List array
        for (int i = 0; i < jsonarr_s.size(); i++) {
            Map<Object,Object> map = new HashMap<>();
            JSONObject jsonobj_1 = (JSONObject) jsonarr_s.get(i);
            JSONObject jive = (JSONObject) jsonobj_1.get("jive");

        Object names = jsonobj_1.get("displayName");
        Object userid = jive.get("username");
        String UserId = userid.toString();


            //return the map with the key value pairs

            int jiracount = count.getJiraCount(UserId);
            map.put("Name", names);
            map.put("UserId", userid);
            map.put("count", jiracount);
            List.add(map);

        }

        return List;
    } 
}

測試類別:

       public class ReporteesListTesting {
   ReporteesService Reportee_Service=null;
   ReporteeList Reportee_List = mock(ReporteeList.class);
   GetEmpId     empid         = mock(GetEmpId.class);
   @Before
   public void setup() {
   Reportee_Service = new ReporteesService(empid);

    }
       @Test
   public void testReporteeList() throws Exception {

    when(Reportee_List.getReportees(54591)).thenReturn("");
    assertEquals("",Reportee_Service.getList("vb256121"));

}

 }

現在,在“返回”部分中,我必須返回列表,因為我的getReportees()返回了列表。

該列表包含以下數據:

  "[{UserId=at1234,count=0,Name=Amrith Taj}, 
    {UserId=AR1234,count=1,Name=Anaga R}, 
    {UserId=MS1234,count=4,Name=Madhu S}]"  

請讓我知道如何完成此操作,以及我是否走在正確的軌道上。請幫助,我是Junits Mockito的新手。

Reportee_Service.getList(“ vb256121”)返回地圖列表,而不是字符串。

when(Reportee_List.getReportees(54591)).thenReturn(new ArrayList<>());
assertEquals(0 ,Reportee_Service.getList("vb256121").size());

您可以為更長的查詢執行此操作

    List<Map<Object, Object>> users = new ArrayList<>();
    Map<Object, Object> map = new HashMap<>();
    map.put("Name", "Amrith Taj");
    map.put("UserId", "at1234");
    map.put("count", "0");
    users.add(map);
    map = new HashMap<>();
    map.put("Name", "Anaga R");
    map.put("UserId", "AR1234");
    map.put("count", "1");
    users.add(map);
    map = new HashMap<>();
    map.put("Name", "Anaga R");
    map.put("UserId", "MS1234");
    map.put("count", "4");
    users.add(map);
    when(Reportee_List.getReportees(54591)).thenReturn(users);
    assertEquals(3 ,Reportee_Service.getList("vb256121").size());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM