I am trying to write a test case for the service using Junit Mockito.
Controller class:
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 class:
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;
}
}
Test class:
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"));
}
}
Now in the Return part I have to return the list, as my getReportees()
return the list.
The List contains this data:
"[{UserId=at1234,count=0,Name=Amrith Taj},
{UserId=AR1234,count=1,Name=Anaga R},
{UserId=MS1234,count=4,Name=Madhu S}]"
Please let me know how can this be done,and whether I am on the right track.Please help,I am new to Junits Mockito.
Reportee_Service.getList("vb256121") returns a List of Maps, not a string.
when(Reportee_List.getReportees(54591)).thenReturn(new ArrayList<>());
assertEquals(0 ,Reportee_Service.getList("vb256121").size());
You can do this for a longer query
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());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.