diff --git a/spring-web/src/test/java/org/springframework/web/client/support/RestClientAdapterTests.java b/spring-web/src/test/java/org/springframework/web/client/support/RestClientAdapterTests.java index 4abb8defbd4..809bbbb227b 100644 --- a/spring-web/src/test/java/org/springframework/web/client/support/RestClientAdapterTests.java +++ b/spring-web/src/test/java/org/springframework/web/client/support/RestClientAdapterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -91,38 +91,39 @@ class RestClientAdapterTests { public static Stream arguments() { return Stream.of( - args((url, observationRegistry) -> { - RestClient restClient = RestClient.builder().baseUrl(url).observationRegistry(observationRegistry).build(); + createArgsForAdapter((url, or) -> { + RestClient restClient = RestClient.builder().baseUrl(url).observationRegistry(or).build(); return RestClientAdapter.create(restClient); }), - args((url, observationRegistry) -> { + createArgsForAdapter((url, or) -> { RestTemplate restTemplate = new RestTemplate(); - restTemplate.setObservationRegistry(observationRegistry); + restTemplate.setObservationRegistry(or); restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory(url)); return RestTemplateAdapter.create(restTemplate); })); } @SuppressWarnings("resource") - private static Object[] args(BiFunction adapterFactory) { + private static Object[] createArgsForAdapter( + BiFunction adapterFactory) { + MockWebServer server = new MockWebServer(); MockResponse response = new MockResponse(); response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!"); server.enqueue(response); - TestObservationRegistry observationRegistry = TestObservationRegistry.create(); + TestObservationRegistry registry = TestObservationRegistry.create(); - HttpExchangeAdapter adapter = adapterFactory.apply(server.url("/").toString(), observationRegistry); + HttpExchangeAdapter adapter = adapterFactory.apply(server.url("/").toString(), registry); Service service = HttpServiceProxyFactory.builderFor(adapter).build().createClient(Service.class); - return new Object[] { server, service, observationRegistry }; + return new Object[] { server, service, registry }; } @ParameterizedAdapterTest - void greeting( - MockWebServer server, Service service, TestObservationRegistry observationRegistry) throws Exception { + void greeting(MockWebServer server, Service service, TestObservationRegistry registry) throws Exception { String response = service.getGreeting(); @@ -130,13 +131,12 @@ class RestClientAdapterTests { assertThat(response).isEqualTo("Hello Spring!"); assertThat(request.getMethod()).isEqualTo("GET"); assertThat(request.getPath()).isEqualTo("/greeting"); - assertThat(observationRegistry).hasObservationWithNameEqualTo("http.client.requests").that() + assertThat(registry).hasObservationWithNameEqualTo("http.client.requests").that() .hasLowCardinalityKeyValue("uri", "/greeting"); } @ParameterizedAdapterTest - void greetingById( - MockWebServer server, Service service, TestObservationRegistry observationRegistry) throws Exception { + void greetingById(MockWebServer server, Service service, TestObservationRegistry registry) throws Exception { ResponseEntity response = service.getGreetingById("456"); @@ -145,13 +145,12 @@ class RestClientAdapterTests { assertThat(response.getBody()).isEqualTo("Hello Spring!"); assertThat(request.getMethod()).isEqualTo("GET"); assertThat(request.getPath()).isEqualTo("/greeting/456"); - assertThat(observationRegistry).hasObservationWithNameEqualTo("http.client.requests").that() + assertThat(registry).hasObservationWithNameEqualTo("http.client.requests").that() .hasLowCardinalityKeyValue("uri", "/greeting/{id}"); } @ParameterizedAdapterTest - void greetingWithDynamicUri( - MockWebServer server, Service service, TestObservationRegistry observationRegistry) throws Exception { + void greetingWithDynamicUri(MockWebServer server, Service service, TestObservationRegistry registry) throws Exception { URI dynamicUri = server.url("/greeting/123").uri(); Optional response = service.getGreetingWithDynamicUri(dynamicUri, "456"); @@ -160,7 +159,7 @@ class RestClientAdapterTests { assertThat(response.orElse("empty")).isEqualTo("Hello Spring!"); assertThat(request.getMethod()).isEqualTo("GET"); assertThat(request.getRequestUrl().uri()).isEqualTo(dynamicUri); - assertThat(observationRegistry).hasObservationWithNameEqualTo("http.client.requests").that() + assertThat(registry).hasObservationWithNameEqualTo("http.client.requests").that() .hasLowCardinalityKeyValue("uri", "none"); } @@ -308,8 +307,8 @@ class RestClientAdapterTests { ResponseEntity getWithUriBuilderFactory(UriBuilderFactory uriBuilderFactory); @GetExchange("/greeting/{id}") - ResponseEntity getWithUriBuilderFactory(UriBuilderFactory uriBuilderFactory, - @PathVariable String id, @RequestParam String param); + ResponseEntity getWithUriBuilderFactory( + UriBuilderFactory uriBuilderFactory, @PathVariable String id, @RequestParam String param); @GetExchange("/greeting") ResponseEntity getWithIgnoredUriBuilderFactory(URI uri, UriBuilderFactory uriBuilderFactory); diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/support/WebClientAdapterTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/support/WebClientAdapterTests.java index 9579267f94f..b5661a49b84 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/support/WebClientAdapterTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/support/WebClientAdapterTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -90,8 +90,7 @@ class WebClientAdapterTests { @Test void greeting() { - prepareResponse(response -> - response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!")); + prepareResponse(response -> response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!")); StepVerifier.create(initService().getGreeting()) .expectNext("Hello Spring!") @@ -111,8 +110,7 @@ class WebClientAdapterTests { }) .build(); - prepareResponse(response -> - response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!")); + prepareResponse(response -> response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!")); StepVerifier.create(initService(webClient).getGreetingWithAttribute("myAttributeValue")) .expectNext("Hello Spring!") @@ -154,8 +152,8 @@ class WebClientAdapterTests { prepareResponse(response -> response.setResponseCode(201)); String fileName = "testFileName"; String originalFileName = "originalTestFileName"; - MultipartFile file = new MockMultipartFile(fileName, originalFileName, - MediaType.APPLICATION_JSON_VALUE, "test".getBytes()); + MultipartFile file = new MockMultipartFile( + fileName, originalFileName, MediaType.APPLICATION_JSON_VALUE, "test".getBytes()); initService().postMultipart(file, "test2"); @@ -255,12 +253,11 @@ class WebClientAdapterTests { String getWithUriBuilderFactory(UriBuilderFactory uriBuilderFactory); @GetExchange("/greeting/{id}") - String getWithUriBuilderFactory(UriBuilderFactory uriBuilderFactory, - @PathVariable String id, @RequestParam String param); + String getWithUriBuilderFactory( + UriBuilderFactory uriBuilderFactory, @PathVariable String id, @RequestParam String param); @GetExchange("/greeting") String getWithIgnoredUriBuilderFactory(URI uri, UriBuilderFactory uriBuilderFactory); - } }