2

Having Rest Client program: - Create client object using Jersey libraries - Invokes the external rest services using URL. - read the response and process the response.

public String getResourceFromService() {
    ClientConfig clientConfig = new DefaultClientConfig();
    Client restClient = Client.create(clientConfig);
    WebResource webResource = restClient.resource(serviceURL)

    ClientResponse response = webResource.accept("application/xml")
            .header("Authorization", "Basic " + credentials)            
            .get(ClientResponse.class);

    //response data 
    if(response.getStatus()== HttpStatus.SC_OK) {
        String responseInXMLString = response.getEntity(String.class);
        return responseInXMLString;
    }
}

Please suggest, What and how to implement Junit for this program.

soufrk
  • 825
  • 1
  • 10
  • 24
sunjavax
  • 91
  • 1
  • 6
  • Jersey releases come with their testing framework as well. Here's [link](https://jersey.java.net/documentation/latest/test-framework.html) for the latest release. – soufrk Feb 03 '17 at 07:46
  • Consider using something like WireMock – tddmonkey Feb 03 '17 at 08:07
  • Jersey Test Framework originated as an internal tool used for verifying the correct implementation of **server-side** components. Here junit for client against server. – sunjavax Feb 03 '17 at 16:06
  • Understood your problem now. If one rest call is inside another, you will have to think about mocking `ClientConfig` somehow, I guess. Seems like other people have hit this problem as will like [here](http://stackoverflow.com/questions/33903050/jersey-client-with-mockito), [and here](http://stackoverflow.com/questions/32192095/mocking-jersey-client-using-mockito). – soufrk Feb 06 '17 at 07:43

0 Answers0