WebTestClient base path is not set to the application context path #24168
Comments
Are you referring to the |
Yes |
@20fps this can be done currently as follows: MockMvcBuilders.webAppContextSetup(this.wac)
.defaultRequest(get("/").contextPath("/myapp"))
.build(); This should work well as long as you want the same context path for all tests in the fixture but it would be less convenient for a one-off test case. I suppose we could expose convenience methods directly on |
On second thought, exposing a convenience method in So I'm closing this without any further changes for now in favor of the sample shown above which is also consistent with how it's set at the |
@rstoyanchev what do you mean by |
I misunderstood the scenario. Disregard my previous comments.
Can you clarify where this is hard-coded? |
Sure
|
@20fps I have a hard time following this. Also, I'm not sure I understand how Maybe you could provide two code snippets, one Thanks! |
@bclozel sure, here is an example: @SpringBootTest(
webEnvironment = WebEnvironment.RANDOM_PORT,
classes = SampleApplicationTest.SampleApplication.class)
@TestPropertySource(properties = "server.servlet.context-path=/test")
class SampleApplicationTest {
@Autowired
private TestRestTemplate testRestTemplate;
@Autowired
private WebTestClient webTestClient;
@Test
void shouldReturnResourcesUsingTestRestTemplate() {
var actual = testRestTemplate.getForEntity("/resources", String.class);
assertThat(actual.getStatusCode()).isEqualTo(OK);
assertThat(actual.getBody()).isEqualTo("test-resource");
}
// will fail with 404 cos context path is not configured
@Test
void shouldReturnResourcesUsingWebTestClient() {
webTestClient.get().uri("/resources").exchange()
.expectStatus().isOk()
.expectBody(String.class).isEqualTo("test-resource");
}
@SpringBootApplication
static class SampleApplication {
static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}
@RestController
static class Controller {
@GetMapping("/resources")
String resources() {
return "test-resource";
}
}
}
} I'd like to have possibility to customize baseUrl for From the example above, let's assume random port will be
I'd like to have possibility to customize base path as generated_host + custom_path without having to create |
For
TestRestClient
context path is autoconfigured as base by default, it is a bit confusing to have both clients autoconfigured with the different base paths.Also I don't see any possibility to configure base path for
WebTestClient
since it is hardcoded on creation:return builder.baseUrl(baseUrl).build();
Now I understand why
servlet.contextPath
is not used since it is not reactive based, but if it is possible to use for non-reactive it should be at least configurable.I'd like to have possibility to customize base path for
WebTestClient
.The text was updated successfully, but these errors were encountered: