如何为返回java列表的restful服务编写测试用例
我正在尝试使用Junit Mockito为服务编写一个测试用例 控制器类:如何为返回java列表的restful服务编写测试用例,java,Java,我正在尝试使用Junit Mockito为服务编写一个测试用例 控制器类: public class ReporteesService { ReporteeList reportee = new ReporteeList(); GetEmpId EmpId = new GetEmpId(); public ReporteesService(ReporteeList reportee) { this.reportee =
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);
}
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());
when(Reportee_List.getReportees(54591)).thenReturn(newarraylist());
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());
List users=new ArrayList();
Map Map=newhashmap();
地图放置(“名称”、“阿姆利特泰姬陵”);
map.put(“UserId”、“at1234”);
地图放置(“计数”、“0”);
添加(地图);
map=新的HashMap();
地图放置(“名称”、“阿纳加R”);
map.put(“UserId”、“AR1234”);
地图放置(“计数”、“1”);
添加(地图);
map=新的HashMap();
地图放置(“名称”、“阿纳加R”);
map.put(“UserId”、“MS1234”);
地图放置(“计数”、“4”);
添加(地图);
when(Reportee_List.getReportees(54591))。然后return(users);
assertEquals(3,Reportee_Service.getList(“vb256121”).size());
Reportee_Service.getList(“vb256121”)返回地图列表,而不是字符串
when(Reportee_List.getReportees(54591)).thenReturn(new ArrayList<>());
assertEquals(0 ,Reportee_Service.getList("vb256121").size());
when(Reportee_List.getReportees(54591)).thenReturn(newarraylist());
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());
List users=new ArrayList();
Map Map=newhashmap();
地图放置(“名称”、“阿姆利特泰姬陵”);
map.put(“UserId”、“at1234”);
地图放置(“计数”、“0”);
添加(地图);
map=新的HashMap();
地图放置(“名称”、“阿纳加R”);
map.put(“UserId”、“AR1234”);
地图放置(“计数”、“1”);
添加(地图);
map=新的HashMap();
地图放置(“名称”、“阿纳加R”);
map.put(“UserId”、“MS1234”);
地图放置(“计数”、“4”);
添加(地图);
when(Reportee_List.getReportees(54591))。然后return(users);
assertEquals(3,Reportee_Service.getList(“vb256121”).size());
您的实际问题是什么?您希望当前代码做什么?它能做到吗?我正在尝试使用mockito为控制器类中的getList()编写一个测试用例。我的疑问是该函数返回列表的位置,因此我如何才能为此编写一个测试用例。请提供任何编写此类测试用例的示例。感谢控制器测试我建议使用WireMock。您的实际问题是什么?您希望当前代码做什么?它能做到吗?我正在尝试使用mockito为控制器类中的getList()编写一个测试用例。我的疑问是该函数返回列表的位置,因此我如何才能为此编写一个测试用例。请提供任何编写此类测试用例的示例。感谢我建议使用wiremock进行控制器测试