'How to setServletPath() in Spring Junit WebTestClient?

@SpringBootTest(properties = "spring.mvc.servlet.path=/test/path")
@AutoConfigureMockMvc
public class MyTest {
   @Autowired
   private WebTestClient webTestClient

   @Test
   public void test() {
       webTestClient.post()
                .uri(URL)
                .bodyValue(json)
                .exchange()
                .expectStatus().isOk()
                .expectBody(String.class)
                .returnResult()
                .getResponseBody();
   }
}

@RestController
public class MyController {    
    @PostMapping
    public Object post(HttpServletRequest req) {
        System.out.println(req.getServletPath()); //always empty in tests
    }
}

This creates a MockHttpServletRequest that is send to the @RestContoller servlets.

Problem: my servlets make use of HttpServletRequest.getServletPath(), but which is always empty using the WebTestClient approach above.

Question: how can I explicit set the servletPath in my junit tests?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source