Test Micronaut Health endpoint

By Sergio del Amo - Thu, 6 May 2021 08:05:17 GMT

Tags: #micronaut #test #spock

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

@MicronautTest
class HealthSpec extends Specification {

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

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