I want to test a class which calls a RESTEasy webservice. It seems Junit doesn’t call the webservice. I checked at server side, the request didnt reach.
public class EmpService {
public List<Employee> getEmployees() {
ClientRequest request = new ClientRequest("http://localhost:8080/rest/employees");
request.accept(MediaType.APPLICATION_JSON);
ClientResponse<Employees> response = null;
try {
response = request.get(Employees.class);
} catch (Exception e) {
e.printStackTrace();
}
Employees received = response.getEntity();
if (received.getEmployees() == null){
System.out.println("Employees is null");
}
return received.getEmployees();
}
}
My intention is not to test the REST service, it is to test the class EmpService. So I have written the following JUnit test case.
public class EmpServiceTest {
@Test
public void testGetEmployees() throws Exception {
EmpService service = new EmpService();
List<Employees> employees = service.getEmployees();
Assert.assertNotNull("Employees is null", employees);
}
@Test
public void testREST() {
ClientRequest request = new ClientRequest("http://localhost:8080/rest/employees");
request.accept(MediaType.APPLICATION_JSON);
ClientResponse<Employees> response = null;
try {
response = request.get(Employees.class);
} catch (Exception e) {
e.printStackTrace();
}
Employees received = response.getEntity();
Assert.assertNotNull("Employees is null", received.getEmployees());
}
}
I tested the REST service in browser and it is working fine. But Junit test case fails to call it and no error reported.
I tried to test the REST service directly in JUnit.
I found out that the problem is with the content-type. The JUnit side doesn’t have a json mapper class. Used jackson json library and it worked fine.