Micronaut-core: Injecting a RxHttpClient in a Junit Test with Kotlin throws io.micronaut.http.client.exceptions.NoHostException

Created on 31 May 2020  Â·  3Comments  Â·  Source: micronaut-projects/micronaut-core

Using Junit and Kotlin, the following test throws NoHostException

@MicronautTest
class BasicAuthTest {

    @Inject
    @Client("/")
    lateinit var client: RxHttpClient

    @Test
    fun verifyHttpBasicAuthWorks() {
        val e = Executable { client.toBlocking().exchange<Any, Any>(HttpRequest.GET<Any>("/").accept(MediaType.TEXT_PLAIN)) } // <3>
       ....
}

It works for Java / Junit

@MicronautTest
public class BasicAuthTest {

    @Inject
    @Client("/")
    RxHttpClient client; 

    @Test
    void verifyHttpBasicAuthWorks() {
        Executable e = () -> client.toBlocking().exchange(HttpRequest.GET("/").accept(MediaType.TEXT_PLAIN)); 

It works for Groovy with Spock

@MicronautTest
class BasicAuthSpec extends Specification {

    @Inject
    @Client("/")
    RxHttpClient client

    def "by default every endpoint is secured"() {
        when: 'Accessing a secured URL without authenticating'
        client.toBlocking().exchange(HttpRequest.GET('/'))

This works for Kotlin

@MicronautTest
class BasicAuthTest {

    @Inject
    lateinit var embeddedServer: EmbeddedServer

    @Test
    fun verifyHttpBasicAuthWorks() {
        val client : RxHttpClient = embeddedServer.applicationContext.createBean(RxHttpClient::class.java, embeddedServer.url) 
         val e = Executable { client.toBlocking().exchange<Any, Any>(HttpRequest.GET<Any>("/").accept(MediaType.TEXT_PLAIN)) } // <3>

Sample project: https://github.com/micronaut-guides/micronaut-security-basicauth-kotlin

Micronaut version: 2.0.0.BUILD-SNAPSHOT

notabug kotlin bug

Most helpful comment

Has to be @field:Client(“/“)

All 3 comments

Has to be @field:Client(“/“)

@graemerocher Why has to be @field:Client("/")??

Ok, I saw in the docs, thanks!

Was this page helpful?
0 / 5 - 0 ratings