PDStat
PDStat

Reputation: 5825

Spring boot integration testing Jersey JAX-RS resource

I am using Spring Boot 2.0.3.RELEASE and creating a simple Jersey JAX-RS resource (using the jersey spring boot starter). I would like to do a simple integration test using a Jersey Client class I have written, it doesn't seem however that the resource has started as I get a java.net.ConnectException: Connection refused

So here we go, first the simple resource

@Component
@Path(Constants.STATUS_PATH)
public class StatusResource {

    private static final String OK = "OK";

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public Response getStatus() {
        return Response.ok(OK).build();
    }

}

The Jersey config

@Component
public class JerseyConfig extends ResourceConfig {

    private static final Logger logger = LoggerFactory.getLogger(JerseyConfig.class);

    public JerseyConfig() {
        logger.debug("Registering JAX-RS resources");
        register(StatusResource.class);
    }

}

The Spring boot application class

@SpringBootApplication
public class Application {

    private static final Logger logger = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) {
        logger.info("==== Starting Services ====");
        SpringApplication.run(Application.class, args);
    }

}

The Jersey Client

public class ServicesClient {

    private static final Logger logger = LoggerFactory.getLogger(ServicesClient.class);

    private Client client;

    public ServicesClient() {
        client = initJerseyClient();
    }

    private Client initJerseyClient() {
        logger.debug("Initialising Jersey Client");
        ClientConfig jerseyClientConfig = new ClientConfig();
        return ClientBuilder.newClient(jerseyClientConfig);
    }

    /**
     * Returns OK if services are running
     * 
     * @return service status
     */
    public String getServicesStatus() {
        logger.debug("Client get services status request");

        Response respone = client.target("http://localhost:8080").path(Constants.STATUS_PATH).request().get();

        return respone.readEntity(String.class);
    }
}

And finally the Spring Boot integration test

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Application.class)
public class StatusResourceTest {

    private static Logger logger = LoggerFactory.getLogger(StatusResourceTest.class);

    private static ServicesClient servicesClient;

    @BeforeAll
    public static void setup() throws IOException {
        logger.debug("--- Starting StatusResourceTest setup ---");
        System.setProperty("services.client.file", "classpath:com/zurich/utils/services/config/services.yaml");
        servicesClient = new ServicesClientBuilder().build();
    }

    @Test
    public void testGetStatus() {
        logger.debug("--- Starting testGetStatus ---");

        String status = servicesClient.getServicesStatus();

        assertThat("Service status should be OK", status, equalTo("OK"));
    }

}

Like I said when the test runs and get is called inside the client, I get the following stack trace. Which to me implies the resource isn't running at http://localhost:8080 but I don't know why.

javax.ws.rs.ProcessingException: java.net.ConnectException: Connection refused: connect
    at org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:284)
    at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:278)
    at org.glassfish.jersey.client.JerseyInvocation.lambda$invoke$0(JerseyInvocation.java:753)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:316)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:298)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:229)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:414)
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:752)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:419)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:319)
    at com.xxx.utils.services.client.ServicesClient.getServicesStatus(ServicesClient.java:178)
    at sit.resource.status.StatusResourceTest.testGetStatus(StatusResourceTest.java:39)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
    at org.glassfish.jersey.client.internal.HttpUrlConnector._apply(HttpUrlConnector.java:390)
    at org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:282)
    ... 11 more

Upvotes: 2

Views: 3581

Answers (1)

PDStat
PDStat

Reputation: 5825

It appears the issue was with the @SpringBootTest annotation, I changed it to the following and it is now working

@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT)

Upvotes: 2

Related Questions