2012-08-12 111 views
0

我想测试调用RESTEasy Web服务的类。看起来Junit不会调用web服务。我在服务器端检查,请求没有达到。Junit不会调用REST Web服务

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服务。

回答

0

我发现问题与内容类型有关。 JUnit方面没有json映射器类。使用杰克逊json库,它工作正常。