我已经为RestTemplate(RestTemplateInterceptor.class)实现了一个拦截器:@Autowiredprivate RestConfigurations configurations;@Beanpublic RestTemplate restTemplate(RestTemplateBuilder builder) { return builder.additionalInterceptors((ClientHttpRequestInterceptor) (httpRequest, bytes, clientHttpRequestExecution) -> { HttpHeaders headers = httpRequest.getHeaders(); headers.add("X-API-KEY", configurations.getApiKey()); return clientHttpRequestExecution.execute(httpRequest,bytes); }).build();}现在我想测试一下。:) 一次尝试看起来像这样:@ExtendWith(SpringExtension.class)@RestClientTest({RestTemplateInterceptor.class, RestConfigurations.class})@AutoConfigureMockMvc@SpringBootTestclass MyTestClass {public static final String URL = "http://testMe.com/";@AutowiredMockRestServiceServer mockServer;@AutowiredRestTemplateBuilder restTemplateBuilder;@Test@DisplayName("Should include header X-API-KEY")void headerShouldContainsXApiKey() throws Exception { mockServer.expect(requestTo(URL)).andRespond(withSuccess("Hello world", TEXT_PLAIN)); String body = restTemplateBuilder.build().exchange(URL// , GET, // null, // String.class).getBody(); assertThat(body).isEqualTo("Hello world"); mockServer.expect(header("X-API-KEY", ""));}但我失败了:java.lang.AssertionError: 预期没有进一步的请求:HTTP GET http://test.hornbach.de/ 0 个请求已执行。任何人的提示?谢谢
添加回答
举报
0/150
提交
取消