javaspring-bootspring-mvcspring-webfluxwebtestclient

Connection refused in integration test after migration from `@WebFluxTest` to `@SpringBootTest`


I do the migration from a Spring Boot reactive to mvc. I migrated controllers and now I try to migrate integration tests.

Controller's tests are annotated like this and if I run test it works.

@RunWith(SpringRunner.class)
@WebFluxTest
public class MyIntegrationTest {
}

Then I replace WebFluxTest annotation like this

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
public class MyIntegrationTest {
}

If I run this test I have reactor.core.Exceptions$ReactiveException: io.netty.channel.AbstractChannel$AnnotatedConnectException: finishConnect(..) failed: Connection refused: localhost/127.0.0.1:8080. Any ideas how to fix it?


Solution

  • @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    

    Will load and start your full application on a random port.

    @AutoConfigureWebTestClient
    

    Will load up a mock of your application and then configure a WebTestClient to go to that mock.

    You are telling Spring to start an application AND to load a mock.

    All this is explained very good in the documentation so please read there and then decide if you wish to load your entire application, or just mocks of it while executing your tests.

    Testing Spring boot applications

    Tests with a running server

    Tests with a mock environment