[英]Spring boot - integration testing - WebTestClient & HttpServletRequest
I'm having difficulties figuring this out. 我很难弄清楚这一点。
I can mock almost everything but for some reason the HttpServletRequest
is mocked but not injected into the @ControllerAdvice
@ExceptionHandler
method. 我几乎可以模拟所有内容,但出于某种原因,可以模拟
HttpServletRequest
,但不能将其注入@ControllerAdvice
@ExceptionHandler
方法。
Any ideas? 有任何想法吗? Thank you for your help in advance!
提前谢谢你的帮助!
STR Repo with minimal plug and play test suite / code STR Repo具有最少的即插即用测试套件/代码
https://github.com/krodyrobi/spring-integration-test-str https://github.com/krodyrobi/spring-integration-test-str
@Component
public class Config {
private final String url = "https://httpstat.us/";
public String getUrl() {
return url;
}
}
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(WebClientResponseException.class)
public ResponseEntity<String> handleException(HttpServletRequest request, WebClientResponseException ex) {
return new ResponseEntity<>(request.getRequestURL() + " " + ex.getResponseBodyAsString(), ex.getStatusCode());
}
}
@RestController
public class SomeController {
private final Config config;
@Autowired
public SomeController(Config config) {
this.config = config;
}
@GetMapping("/test")
public Mono<String> test() {
return WebClient
.create(config.getUrl())
.get()
.uri("/200")
.retrieve()
.bodyToMono(String.class);
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {
SomeController.class,
GlobalExceptionHandler.class,
})
public class SomeControllerTest {
private final static String baseUrl = "http://localhost:9999/";
public @Rule WireMockRule wireMockRule = new WireMockRule(9999);
private @MockBean Config config;
private @MockBean HttpServletRequest request;
private WebTestClient webClient;
private @Autowired SomeController controller;
private @Autowired GlobalExceptionHandler exceptionHandler;
@Before
public void setUp() {
webClient = WebTestClient
.bindToController(controller)
.controllerAdvice(exceptionHandler)
.build();
when(config.getUrl()).thenReturn(baseUrl);
}
@Test
public void test_works() {
wireMockRule
.stubFor(get(urlEqualTo("/200"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "text/plain")
.withBody("200 MOCK")));
webClient
.get()
.uri("/test")
.exchange()
.expectStatus()
.isOk()
.expectBody(String.class)
.isEqualTo("200 MOCK");
wireMockRule.verify(getRequestedFor(urlMatching("/200")));
}
@Test
public void test_fails() {
// java.lang.IllegalStateException: No suitable resolver for argument 0
// of type 'javax.servlet.http.HttpServletRequest' on public
// org.springframework.http.ResponseEntity<java.lang.String>
// com.example.demo.GlobalExceptionHandler.handleException(
// javax.servlet.http.HttpServletRequest,
// ...client.WebClientResponseException
// )
wireMockRule
.stubFor(get(urlEqualTo("/200"))
.willReturn(aResponse()
.withStatus(404)
.withHeader("Content-Type", "text/plain")
.withBody("404 MOCK")));
webClient
.get()
.uri("/test")
.exchange()
.expectStatus()
.isNotFound()
.expectBody(String.class)
.isEqualTo("Http://localhost:8080/test 404 MOCK");
wireMockRule.verify(getRequestedFor(urlMatching("/200")));
}
}
Use below instead of HttpServletRequest
在下面使用而不是
HttpServletRequest
import org.springframework.http.server.reactive.ServerHttpRequest;
ServerHttpRequest request
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.