Dude, r u 200 OK?

One of the great features of Spring Boot is Actuator and its health endpoint. It gives you an overview how healthy your app is.

The context starts, but what's about the health of the app?

Configure Rest Assured

To check the health endpoint of our app, we will use the RestAssured library.

But before using it, we need to configure it. Add the following to your abstract test class because we will share it between the tests:

protected RequestSpecification requestSpecification;

@LocalServerPort
private int localServerPort;

@Before
public void setUpAbstractIntegrationTest() {
    requestSpecification = new RequestSpecBuilder()
            .setPort(localServerPort)
            .addHeader(
                    HttpHeaders.CONTENT_TYPE,
                    MediaType.APPLICATION_JSON_VALUE
            )
            .build();
}

Here we ask Spring Boot to inject the random port it received at the start of the app, so that we can pre-configure the RestAssured's requestSpecification.

Call the endpoint

Now let's check if the app is actually healthy by doing the following in our DemoApplicationTest:

@Test
public void healthy() {
    given(requestSpecification)
            .when()
            .get("/actuator/health")
            .then()
            .statusCode(200)
            .log().ifValidationFails(LogDetail.ALL);
}

If we run it, it will fail:

HTTP/1.1 503 Service Unavailable
transfer-encoding: chunked
Content-Type: application/vnd.spring-boot.actuator.v2+json;charset=UTF-8

{
    "status": "DOWN",
    "details": {
        "diskSpace": { ... },
        "redis": {
            "status": "DOWN",
            "details": {
                "error": "org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379"
            }
        },
        "db": {
            "status": "UP",
            "details": {
                "database": "PostgreSQL",
                "hello": 1
            }
        }
    }
}

Expected status code <200> but was <503>.

It seems that it couldn't find Redis and there is no in-memory option for it.

results matching ""

    No results matching ""