From 383d6c897f9bff62c5ffe42e763eb1cd6f25dd1a Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Tue, 25 Oct 2022 16:15:51 +0200 Subject: [PATCH] Remove reflection for SimpleRequestExpectationManager creation Closes gh-32867 --- .../MockServerRestTemplateCustomizer.java | 19 ++++++++++----- ...MockServerRestTemplateCustomizerTests.java | 23 ++++++++++++++++++- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/MockServerRestTemplateCustomizer.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/MockServerRestTemplateCustomizer.java index 6f180f6a6d..2dc186290d 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/MockServerRestTemplateCustomizer.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/MockServerRestTemplateCustomizer.java @@ -19,6 +19,7 @@ package org.springframework.boot.test.web.client; import java.util.Collections; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Supplier; import org.springframework.beans.BeanUtils; import org.springframework.boot.web.client.RestTemplateBuilder; @@ -47,27 +48,33 @@ import org.springframework.web.client.RestTemplate; * the related server. * * @author Phillip Webb + * @author Moritz Halbritter * @since 1.4.0 * @see #getServer() * @see #getServer(RestTemplate) */ public class MockServerRestTemplateCustomizer implements RestTemplateCustomizer { - private Map expectationManagers = new ConcurrentHashMap<>(); + private final Map expectationManagers = new ConcurrentHashMap<>(); - private Map servers = new ConcurrentHashMap<>(); + private final Map servers = new ConcurrentHashMap<>(); - private final Class expectationManager; + private final Supplier expectationManagerSupplier; private boolean detectRootUri = true; public MockServerRestTemplateCustomizer() { - this.expectationManager = SimpleRequestExpectationManager.class; + this(SimpleRequestExpectationManager::new); } public MockServerRestTemplateCustomizer(Class expectationManager) { + this(() -> BeanUtils.instantiateClass(expectationManager)); Assert.notNull(expectationManager, "ExpectationManager must not be null"); - this.expectationManager = expectationManager; + } + + public MockServerRestTemplateCustomizer(Supplier expectationManagerSupplier) { + Assert.notNull(expectationManagerSupplier, "ExpectationManagerSupplier must not be null"); + this.expectationManagerSupplier = expectationManagerSupplier; } /** @@ -91,7 +98,7 @@ public class MockServerRestTemplateCustomizer implements RestTemplateCustomizer } protected RequestExpectationManager createExpectationManager() { - return BeanUtils.instantiateClass(this.expectationManager); + return this.expectationManagerSupplier.get(); } public MockRestServiceServer getServer() { diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/MockServerRestTemplateCustomizerTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/MockServerRestTemplateCustomizerTests.java index d0ebd4c27e..994c706da4 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/MockServerRestTemplateCustomizerTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/web/client/MockServerRestTemplateCustomizerTests.java @@ -16,6 +16,8 @@ package org.springframework.boot.test.web.client; +import java.util.function.Supplier; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -35,6 +37,7 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat * Tests for {@link MockServerRestTemplateCustomizer}. * * @author Phillip Webb + * @author Moritz Halbritter */ class MockServerRestTemplateCustomizerTests { @@ -55,10 +58,19 @@ class MockServerRestTemplateCustomizerTests { @Test void createWhenExpectationManagerClassIsNullShouldThrowException() { - assertThatIllegalArgumentException().isThrownBy(() -> new MockServerRestTemplateCustomizer(null)) + Class expectationManager = null; + assertThatIllegalArgumentException().isThrownBy(() -> new MockServerRestTemplateCustomizer(expectationManager)) .withMessageContaining("ExpectationManager must not be null"); } + @Test + void createWhenExpectationManagerSupplierIsNullShouldThrowException() { + Supplier expectationManagerSupplier = null; + assertThatIllegalArgumentException() + .isThrownBy(() -> new MockServerRestTemplateCustomizer(expectationManagerSupplier)) + .withMessageContaining("ExpectationManagerSupplier must not be null"); + } + @Test void createShouldUseExpectationManagerClass() { MockServerRestTemplateCustomizer customizer = new MockServerRestTemplateCustomizer( @@ -68,6 +80,15 @@ class MockServerRestTemplateCustomizerTests { .isInstanceOf(UnorderedRequestExpectationManager.class); } + @Test + void createShouldUseSupplier() { + MockServerRestTemplateCustomizer customizer = new MockServerRestTemplateCustomizer( + UnorderedRequestExpectationManager::new); + customizer.customize(new RestTemplate()); + assertThat(customizer.getServer()).extracting("expectationManager") + .isInstanceOf(UnorderedRequestExpectationManager.class); + } + @Test void detectRootUriShouldDefaultToTrue() { MockServerRestTemplateCustomizer customizer = new MockServerRestTemplateCustomizer(