Search code examples
grailsmicronaut

Is it possible to use Micronaut Declarative HTTP client in grails 4 functional API tests?


I am trying to create a Declarative client to test the API I am writing in grails 4, like you can do it in micronaut.

As far as I have come, you can create a declarative client if you know the base URL at compile time. Because the grails functional (integration) tests create a server on a random port, you cannot know that port beforehand and use the @Client('http://localhost:8080') annotation.

Using something like @Client('http://localhost:${local.server.port}') fails with grails complaining that the configuration property cannot be looked up.

Is there something I am missing?


Solution

  • Is it possible to use Micronnaut Declarative HTTP client in grails 4 functional API tests?

    It is.

    A problem with doing that is that when using @Client('/') to connect to the local running server, an EmbeddedServer bean needs to be configured in the context to answer the question of what port the app is running on. Some of the relevant code is at https://github.com/micronaut-projects/micronaut-core/blob/458bbf07221407abe7e283815fb62b7b4d1fc224/http-client-core/src/main/java/io/micronaut/http/client/DefaultLoadBalancerResolver.java#L93.

    We will add testing support to make this easier but you can make it work by registering the bean yourself. See the following:

    That is a kludge for now, but I believe it will work until proper support is released in the testing framework.