0

我想测试一个调用 RESTEasy 网络服务的类。似乎 Junit 没有调用网络服务。我在服务器端检查,请求没有到达。

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();
    }
  }

我的目的不是测试 REST 服务,而是测试类EmpService。所以我写了下面的 JUnit 测试用例。

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());
    }
}

我在浏览器中测试了 REST 服务,它工作正常。但是 Junit 测试用例调用它失败并且没有报告错误。我尝试直接在 JUnit 中测试 REST 服务。

4

1 回答 1

0

我发现问题出在内容类型上。JUnit 端没有 json 映射器类。使用杰克逊 json 库,它工作正常。

于 2012-09-13T14:23:30.107 回答