Diferdin
Diferdin

Reputation: 1260

WebTestClient returns 404 instead of 200

I am testing the REST layer of my reactive SpringBoot (3) app and using Spring's WebTestClient:

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ContextConfiguration(classes = ReferenceDataApplication.class)
public class RefDataResourceTest {

    @Autowired
    private WebTestClient webTestClient;

    @BeforeEach
    void setup() {
        webTestClient = WebTestClient.bindToController(new RefDataResource()).build();
    }

    @Test
    public void shouldGetReferenceDataByTypeCode() {
        webTestClient
                .get()
                .uri("/api/projects/refData/findByCode")
                .exchange()
                .expectStatus().isOk();
    }
}

I defined /api/projects/ as base-path in Spring, and my resource implements the delegate generated by OpenApi generation of the yaml contract:

@Component
public class RefDataResource implements RefDataApiDelegate {

    @Override
    public Mono<ResponseEntity<Flux<RefDataElementDTO>>> findDataByCode(String code,
                                                                              ServerWebExchange exchange) {

        return Mono.just(ResponseEntity.ok().build());
    }

}

When I run the SpringBoot application, I can use Postman to hit the endpoint and I can get the expected 200, however when I use the WebTestClient above I keep getting 404. What am I doing wrong here?

Upvotes: 0

Views: 479

Answers (1)

Diferdin
Diferdin

Reputation: 1260

I got it working in the end by simply binding the WebTestClient to a specific host and port. That configuration was initially missing.

Upvotes: 0

Related Questions