Maven Dependencies

Add these dependencies to your pom.xml

	<dependency>
	  <groupId>org.jboss.resteasy</groupId>
	  <artifactId>resteasy-client</artifactId>
	  <version>3.1.4.Final</version>
	</dependency>
	<dependency>
	  <groupId>org.jboss.resteasy</groupId>
	  <artifactId>resteasy-jackson2-provider</artifactId>
	  <version>3.1.4.Final</version>
	</dependency>

HTTP Client

Create in your client folder Config.java

package com.xyz.checkout.test.client;

import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Config {
    
    @Bean
    public ResteasyClient getHttpClient() {
        return new ResteasyClientBuilder()
                .disableTrustManager()
                .connectionPoolSize(20).build();
    }

}

Web Service Interface

Let’s say we have Billing Address web service.

First we need to describe that web service as an interface. We can 1) create it from scratch knowing the web service contract from docs, 2) copy/paste that from the actual web service code or 3) use the web service code as a dependency. It’s up to you.

This is an example of the interface for the Billing Address Service

package com.xyz.checkout.test.client.billing;

import com.xyz.checkout.test.model.BillingAddressDto;

import javax.ws.rs.Consumes;
import javax.ws.rs.CookieParam;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

@Path("/checkout/billing")
public interface BillingAddressService {

    public static final String APPLICATION_JSON = "application/json";

    @GET
    @Path("/{orderId}")
    @Consumes(APPLICATION_JSON)
    @Produces(APPLICATION_JSON)
    BillingAddressDto getBillingAddress(
            @PathParam("orderId") String orderId,
            @CookieParam("token") String token);

    @PUT
    @Path("/{orderId}")
    @Consumes(APPLICATION_JSON)
    @Produces(APPLICATION_JSON)
    void updateBillingAddress(
            @PathParam("orderId") String orderId,
            BillingAddressDto billingAddressDto,
            @CookieParam("token") String token);

}

Create a package client.billing and save it there

Billing Address Service Client

In client.billing create BillingAddressServiceClientConfig.java

package com.xyz.checkout.test.client.billing;

import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BillingAddressServiceConfig {

    @Autowired
    ResteasyClient httpClient;

    private static final String URL = "https://xyz.com/api";

    @Bean
    public BillingAddressService getBillingAddressService() {
        return httpClient
                .target(URL)
                .proxy(BillingAddressService.class);
    }

}

Now we’re ready to create a client for Billing Address Service

package com.xyz.checkout.test.client.billing;

import com.xyz.checkout.test.model.BillingAddressDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillingAddressServiceClient {

    @Autowired
    BillingAddressService billingAddressService;

    public void updateBillingAddress(String orderId, BillingAddressDto address, String token) {
        billingAddressService.updateBillingAddress(orderId, address, token);
    }

    public BillingAddressDto getBillingAddress(String orderId, String token) {
        return billingAddressService.getBillingAddress(orderId, token);
    }
}

How to use client

Now in any test you can inject your client with the Spring annotation @Autowired

    @Autowired
    public BillingAddressServiceClient billingAddressServiceClient;

and make calls to Billing Address Service

billingAddressServiceClient.updateBillingAddress(orderId, billingAddress, token);
BillingAddressDto updatedAddress = billingAddressServiceClient.getBillingAddress(orderId, token);

Other services

Repeate steps above (except HTTP client - you need to do it only once) for each service that you need to use in your tests.

At the end your project will look just like this

Framework project structure

See also HTTP and RESTEasy for the related info

Back to the top