Test Micronaut Health endpoint

Micronaut management dependency adds support for monitoring of your application via endpoints. For example, the health endpoint exposes the state of your application.

I test it with Spock:

package example.micronaut

import io.micronaut.http.HttpRequest
import io.micronaut.http.client.HttpClient
import io.micronaut.http.client.annotation.Client
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import javax.inject.Inject
import spock.lang.Specification
import io.micronaut.http.HttpResponse
import io.micronaut.http.HttpStatus
import io.micronaut.http.client.BlockingHttpClient

class HealthSpec extends Specification {

    HttpClient httpClient
    void "/health responds OK"() {
        BlockingHttpClient client = httpClient.toBlocking()
        HttpRequest<?> request = HttpRequest.GET('/health')
        HttpResponse<Map> response = client.exchange(request, Map)

        response.status() == HttpStatus.OK
        response.body() == [status: 'UP']

Tags: #micronaut #test #spock
May 2021, 06.