javaspring-bootspring-mvcspring-webfluxwebtestclient

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


I migrated to Spring Boot Reactive from MVC, migrated the Controllers, and am now trying to migrate integration tests.

Controller tests are annotated like this; if I run the 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 an error:

reactor.core.Exceptions$ReactiveException: io.netty.channel.AbstractChannel$AnnotatedConnectException: finishConnect(..) failed: Connection refused: localhost/127.0.0.1:8080

Any ideas on 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.

    This is explained very well in the documentation; so please read that and decide if you wish to load your entire application, or mock it while executing your tests.

    Testing Spring boot applications

    Tests with a running server

    Tests with a mock environment