From 441ed30ee42a8e0075e0417bc9a5042cf7a12be4 Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Thu, 20 Apr 2023 07:37:58 +0100 Subject: [PATCH] Polish "Replace Mockito argument captors with assertArg" Co-authored-by: Andy Wilkinson See gh-35015 --- .../ServiceReadinessChecksTests.java | 14 +- ...rviceConnectionContextCustomizerTests.java | 13 +- .../transport/HttpClientTransportTests.java | 164 +++++++++--------- .../EncodePasswordCommandTests.java | 29 ++-- ...onfigDataEnvironmentContributorsTests.java | 10 +- .../properties/bind/MapBinderTests.java | 6 +- .../SpringProfileModelHandlerTests.java | 66 +++---- 7 files changed, 149 insertions(+), 153 deletions(-) diff --git a/spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/readiness/ServiceReadinessChecksTests.java b/spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/readiness/ServiceReadinessChecksTests.java index 031825be4e6..237aefbf82b 100644 --- a/spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/readiness/ServiceReadinessChecksTests.java +++ b/spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/readiness/ServiceReadinessChecksTests.java @@ -25,7 +25,7 @@ import java.util.Map; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.ArgumentCaptor; +import org.mockito.ArgumentMatchers; import org.springframework.boot.context.properties.bind.Binder; import org.springframework.boot.docker.compose.core.RunningService; @@ -85,12 +85,12 @@ class ServiceReadinessChecksTests { void loadCanResolveArguments() { this.loader = spy(MockSpringFactoriesLoader.class); createChecks(); - ArgumentCaptor captor = ArgumentCaptor.forClass(ArgumentResolver.class); - then(this.loader).should().load(eq(ServiceReadinessCheck.class), captor.capture()); - ArgumentResolver argumentResolver = captor.getValue(); - assertThat(argumentResolver.resolve(ClassLoader.class)).isEqualTo(this.classLoader); - assertThat(argumentResolver.resolve(Environment.class)).isEqualTo(this.environment); - assertThat(argumentResolver.resolve(Binder.class)).isEqualTo(this.binder); + then(this.loader).should() + .load(eq(ServiceReadinessCheck.class), ArgumentMatchers.assertArg((argumentResolver) -> { + assertThat(argumentResolver.resolve(ClassLoader.class)).isEqualTo(this.classLoader); + assertThat(argumentResolver.resolve(Environment.class)).isEqualTo(this.environment); + assertThat(argumentResolver.resolve(Binder.class)).isEqualTo(this.binder); + })); } @Test diff --git a/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/ServiceConnectionContextCustomizerTests.java b/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/ServiceConnectionContextCustomizerTests.java index 25b2d7dad10..47c62115a01 100644 --- a/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/ServiceConnectionContextCustomizerTests.java +++ b/spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/ServiceConnectionContextCustomizerTests.java @@ -21,10 +21,9 @@ import java.util.Map; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.ArgumentCaptor; +import org.mockito.ArgumentMatchers; import org.testcontainers.containers.PostgreSQLContainer; -import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails; @@ -83,12 +82,12 @@ class ServiceConnectionContextCustomizerTests { given(this.factories.getConnectionDetails(this.source, true)) .willReturn(Map.of(JdbcConnectionDetails.class, connectionDetails)); customizer.customizeContext(context, mergedConfig); - ArgumentCaptor beanDefinitionCaptor = ArgumentCaptor.forClass(BeanDefinition.class); then(beanFactory).should() - .registerBeanDefinition(eq("testJdbcConnectionDetailsForTest"), beanDefinitionCaptor.capture()); - RootBeanDefinition beanDefinition = (RootBeanDefinition) beanDefinitionCaptor.getValue(); - assertThat(beanDefinition.getInstanceSupplier().get()).isSameAs(connectionDetails); - assertThat(beanDefinition.getBeanClass()).isEqualTo(TestJdbcConnectionDetails.class); + .registerBeanDefinition(eq("testJdbcConnectionDetailsForTest"), + ArgumentMatchers.assertArg((beanDefinition) -> { + assertThat(beanDefinition.getInstanceSupplier().get()).isSameAs(connectionDetails); + assertThat(beanDefinition.getBeanClass()).isEqualTo(TestJdbcConnectionDetails.class); + })); } @Test diff --git a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/transport/HttpClientTransportTests.java b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/transport/HttpClientTransportTests.java index b2fd9a0053b..40bd62e2a4e 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/transport/HttpClientTransportTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/transport/HttpClientTransportTests.java @@ -33,11 +33,10 @@ import org.apache.hc.core5.http.ClassicHttpResponse; import org.apache.hc.core5.http.HttpEntity; import org.apache.hc.core5.http.HttpHeaders; import org.apache.hc.core5.http.HttpHost; +import org.assertj.core.api.ThrowingConsumer; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.ArgumentCaptor; -import org.mockito.Captor; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @@ -78,9 +77,6 @@ class HttpClientTransportTests { @Mock private InputStream content; - @Captor - private ArgumentCaptor requestCaptor; - private HttpClientTransport http; private URI uri; @@ -117,13 +113,14 @@ class HttpClientTransportTests { given(this.entity.getContent()).willReturn(this.content); given(this.response.getCode()).willReturn(200); Response response = this.http.post(this.uri); - then(this.client).should().executeOpen(any(HttpHost.class), this.requestCaptor.capture(), isNull()); - HttpUriRequest request = this.requestCaptor.getValue(); - assertThat(request).isInstanceOf(HttpPost.class); - assertThat(request.getUri()).isEqualTo(this.uri); - assertThat(request.getFirstHeader(HttpHeaders.CONTENT_TYPE)).isNull(); - assertThat(request.getFirstHeader(HttpClientTransport.REGISTRY_AUTH_HEADER)).isNull(); - assertThat(response.getContent()).isSameAs(this.content); + then(this.client).should() + .executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer) (request) -> { + assertThat(request).isInstanceOf(HttpPost.class); + assertThat(request.getUri()).isEqualTo(this.uri); + assertThat(request.getFirstHeader(HttpHeaders.CONTENT_TYPE)).isNull(); + assertThat(request.getFirstHeader(HttpClientTransport.REGISTRY_AUTH_HEADER)).isNull(); + assertThat(response.getContent()).isSameAs(this.content); + }), isNull()); } @Test @@ -132,13 +129,15 @@ class HttpClientTransportTests { given(this.entity.getContent()).willReturn(this.content); given(this.response.getCode()).willReturn(200); Response response = this.http.post(this.uri, "auth token"); - then(this.client).should().executeOpen(any(HttpHost.class), this.requestCaptor.capture(), isNull()); - HttpUriRequest request = this.requestCaptor.getValue(); - assertThat(request).isInstanceOf(HttpPost.class); - assertThat(request.getUri()).isEqualTo(this.uri); - assertThat(request.getFirstHeader(HttpHeaders.CONTENT_TYPE)).isNull(); - assertThat(request.getFirstHeader(HttpClientTransport.REGISTRY_AUTH_HEADER).getValue()).isEqualTo("auth token"); - assertThat(response.getContent()).isSameAs(this.content); + then(this.client).should() + .executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer) (request) -> { + assertThat(request).isInstanceOf(HttpPost.class); + assertThat(request.getUri()).isEqualTo(this.uri); + assertThat(request.getFirstHeader(HttpHeaders.CONTENT_TYPE)).isNull(); + assertThat(request.getFirstHeader(HttpClientTransport.REGISTRY_AUTH_HEADER).getValue()) + .isEqualTo("auth token"); + assertThat(response.getContent()).isSameAs(this.content); + }), isNull()); } @Test @@ -147,13 +146,14 @@ class HttpClientTransportTests { given(this.entity.getContent()).willReturn(this.content); given(this.response.getCode()).willReturn(200); Response response = this.http.post(this.uri, ""); - then(this.client).should().executeOpen(any(HttpHost.class), this.requestCaptor.capture(), isNull()); - HttpUriRequest request = this.requestCaptor.getValue(); - assertThat(request).isInstanceOf(HttpPost.class); - assertThat(request.getUri()).isEqualTo(this.uri); - assertThat(request.getFirstHeader(HttpHeaders.CONTENT_TYPE)).isNull(); - assertThat(request.getFirstHeader(HttpClientTransport.REGISTRY_AUTH_HEADER)).isNull(); - assertThat(response.getContent()).isSameAs(this.content); + then(this.client).should() + .executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer) (request) -> { + assertThat(request).isInstanceOf(HttpPost.class); + assertThat(request.getUri()).isEqualTo(this.uri); + assertThat(request.getFirstHeader(HttpHeaders.CONTENT_TYPE)).isNull(); + assertThat(request.getFirstHeader(HttpClientTransport.REGISTRY_AUTH_HEADER)).isNull(); + assertThat(response.getContent()).isSameAs(this.content); + }), isNull()); } @Test @@ -164,18 +164,19 @@ class HttpClientTransportTests { given(this.response.getCode()).willReturn(200); Response response = this.http.post(this.uri, APPLICATION_JSON, (out) -> StreamUtils.copy(content, StandardCharsets.UTF_8, out)); - then(this.client).should().executeOpen(any(HttpHost.class), this.requestCaptor.capture(), isNull()); - HttpUriRequest request = this.requestCaptor.getValue(); - HttpEntity entity = request.getEntity(); - assertThat(request).isInstanceOf(HttpPost.class); - assertThat(request.getUri()).isEqualTo(this.uri); - assertThat(entity.isRepeatable()).isFalse(); - assertThat(entity.getContentLength()).isEqualTo(content.length()); - assertThat(entity.getContentType()).isEqualTo(APPLICATION_JSON); - assertThat(entity.isStreaming()).isTrue(); - assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent); - assertThat(writeToString(entity)).isEqualTo(content); - assertThat(response.getContent()).isSameAs(this.content); + then(this.client).should() + .executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer) (request) -> { + HttpEntity entity = request.getEntity(); + assertThat(request).isInstanceOf(HttpPost.class); + assertThat(request.getUri()).isEqualTo(this.uri); + assertThat(entity.isRepeatable()).isFalse(); + assertThat(entity.getContentLength()).isEqualTo(content.length()); + assertThat(entity.getContentType()).isEqualTo(APPLICATION_JSON); + assertThat(entity.isStreaming()).isTrue(); + assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent); + assertThat(writeToString(entity)).isEqualTo(content); + assertThat(response.getContent()).isSameAs(this.content); + }), isNull()); } @Test @@ -186,18 +187,19 @@ class HttpClientTransportTests { given(this.response.getCode()).willReturn(200); Response response = this.http.post(this.uri, APPLICATION_X_TAR, (out) -> StreamUtils.copy(content, StandardCharsets.UTF_8, out)); - then(this.client).should().executeOpen(any(HttpHost.class), this.requestCaptor.capture(), isNull()); - HttpUriRequest request = this.requestCaptor.getValue(); - HttpEntity entity = request.getEntity(); - assertThat(request).isInstanceOf(HttpPost.class); - assertThat(request.getUri()).isEqualTo(this.uri); - assertThat(entity.isRepeatable()).isFalse(); - assertThat(entity.getContentLength()).isEqualTo(-1); - assertThat(entity.getContentType()).isEqualTo(APPLICATION_X_TAR); - assertThat(entity.isStreaming()).isTrue(); - assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent); - assertThat(writeToString(entity)).isEqualTo(content); - assertThat(response.getContent()).isSameAs(this.content); + then(this.client).should() + .executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer) (request) -> { + HttpEntity entity = request.getEntity(); + assertThat(request).isInstanceOf(HttpPost.class); + assertThat(request.getUri()).isEqualTo(this.uri); + assertThat(entity.isRepeatable()).isFalse(); + assertThat(entity.getContentLength()).isEqualTo(-1); + assertThat(entity.getContentType()).isEqualTo(APPLICATION_X_TAR); + assertThat(entity.isStreaming()).isTrue(); + assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent); + assertThat(writeToString(entity)).isEqualTo(content); + assertThat(response.getContent()).isSameAs(this.content); + }), isNull()); } @Test @@ -208,18 +210,18 @@ class HttpClientTransportTests { given(this.response.getCode()).willReturn(200); Response response = this.http.put(this.uri, APPLICATION_JSON, (out) -> StreamUtils.copy(content, StandardCharsets.UTF_8, out)); - then(this.client).should().executeOpen(any(HttpHost.class), this.requestCaptor.capture(), isNull()); - HttpUriRequest request = this.requestCaptor.getValue(); - HttpEntity entity = request.getEntity(); - assertThat(request).isInstanceOf(HttpPut.class); - assertThat(request.getUri()).isEqualTo(this.uri); - assertThat(entity.isRepeatable()).isFalse(); - assertThat(entity.getContentLength()).isEqualTo(content.length()); - assertThat(entity.getContentType()).isEqualTo(APPLICATION_JSON); - assertThat(entity.isStreaming()).isTrue(); - assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent); - assertThat(writeToString(entity)).isEqualTo(content); - assertThat(response.getContent()).isSameAs(this.content); + then(this.client).should().executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer) (request) -> { + HttpEntity entity = request.getEntity(); + assertThat(request).isInstanceOf(HttpPut.class); + assertThat(request.getUri()).isEqualTo(this.uri); + assertThat(entity.isRepeatable()).isFalse(); + assertThat(entity.getContentLength()).isEqualTo(content.length()); + assertThat(entity.getContentType()).isEqualTo(APPLICATION_JSON); + assertThat(entity.isStreaming()).isTrue(); + assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent); + assertThat(writeToString(entity)).isEqualTo(content); + assertThat(response.getContent()).isSameAs(this.content); + }), isNull()); } @Test @@ -230,18 +232,18 @@ class HttpClientTransportTests { given(this.response.getCode()).willReturn(200); Response response = this.http.put(this.uri, APPLICATION_X_TAR, (out) -> StreamUtils.copy(content, StandardCharsets.UTF_8, out)); - then(this.client).should().executeOpen(any(HttpHost.class), this.requestCaptor.capture(), isNull()); - HttpUriRequest request = this.requestCaptor.getValue(); - HttpEntity entity = request.getEntity(); - assertThat(request).isInstanceOf(HttpPut.class); - assertThat(request.getUri()).isEqualTo(this.uri); - assertThat(entity.isRepeatable()).isFalse(); - assertThat(entity.getContentLength()).isEqualTo(-1); - assertThat(entity.getContentType()).isEqualTo(APPLICATION_X_TAR); - assertThat(entity.isStreaming()).isTrue(); - assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent); - assertThat(writeToString(entity)).isEqualTo(content); - assertThat(response.getContent()).isSameAs(this.content); + then(this.client).should().executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer) (request) -> { + HttpEntity entity = request.getEntity(); + assertThat(request).isInstanceOf(HttpPut.class); + assertThat(request.getUri()).isEqualTo(this.uri); + assertThat(entity.isRepeatable()).isFalse(); + assertThat(entity.getContentLength()).isEqualTo(-1); + assertThat(entity.getContentType()).isEqualTo(APPLICATION_X_TAR); + assertThat(entity.isStreaming()).isTrue(); + assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent); + assertThat(writeToString(entity)).isEqualTo(content); + assertThat(response.getContent()).isSameAs(this.content); + }), isNull()); } @Test @@ -250,12 +252,14 @@ class HttpClientTransportTests { given(this.entity.getContent()).willReturn(this.content); given(this.response.getCode()).willReturn(200); Response response = this.http.delete(this.uri); - then(this.client).should().executeOpen(any(HttpHost.class), this.requestCaptor.capture(), isNull()); - HttpUriRequest request = this.requestCaptor.getValue(); - assertThat(request).isInstanceOf(HttpDelete.class); - assertThat(request.getUri()).isEqualTo(this.uri); - assertThat(request.getFirstHeader(HttpHeaders.CONTENT_TYPE)).isNull(); - assertThat(response.getContent()).isSameAs(this.content); + + then(this.client).should() + .executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer) (request) -> { + assertThat(request).isInstanceOf(HttpDelete.class); + assertThat(request.getUri()).isEqualTo(this.uri); + assertThat(request.getFirstHeader(HttpHeaders.CONTENT_TYPE)).isNull(); + assertThat(response.getContent()).isSameAs(this.content); + }), isNull()); } @Test diff --git a/spring-boot-project/spring-boot-tools/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/encodepassword/EncodePasswordCommandTests.java b/spring-boot-project/spring-boot-tools/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/encodepassword/EncodePasswordCommandTests.java index 8820f76255a..d406bf55441 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/encodepassword/EncodePasswordCommandTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/encodepassword/EncodePasswordCommandTests.java @@ -20,8 +20,6 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.ArgumentCaptor; -import org.mockito.Captor; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.boot.cli.command.status.ExitStatus; @@ -31,6 +29,7 @@ import org.springframework.security.crypto.factory.PasswordEncoderFactories; import org.springframework.security.crypto.password.Pbkdf2PasswordEncoder; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.assertArg; import static org.mockito.BDDMockito.then; /** @@ -43,9 +42,6 @@ class EncodePasswordCommandTests { private MockLog log; - @Captor - private ArgumentCaptor message; - @BeforeEach void setup() { this.log = MockLog.attach(); @@ -60,10 +56,10 @@ class EncodePasswordCommandTests { void encodeWithNoAlgorithmShouldUseBcrypt() throws Exception { EncodePasswordCommand command = new EncodePasswordCommand(); ExitStatus status = command.run("boot"); - then(this.log).should().info(this.message.capture()); - assertThat(this.message.getValue()).startsWith("{bcrypt}"); - assertThat(PasswordEncoderFactories.createDelegatingPasswordEncoder().matches("boot", this.message.getValue())) - .isTrue(); + then(this.log).should().info(assertArg((message) -> { + assertThat(message).startsWith("{bcrypt}"); + assertThat(PasswordEncoderFactories.createDelegatingPasswordEncoder().matches("boot", message)).isTrue(); + })); assertThat(status).isEqualTo(ExitStatus.OK); } @@ -71,9 +67,10 @@ class EncodePasswordCommandTests { void encodeWithBCryptShouldUseBCrypt() throws Exception { EncodePasswordCommand command = new EncodePasswordCommand(); ExitStatus status = command.run("-a", "bcrypt", "boot"); - then(this.log).should().info(this.message.capture()); - assertThat(this.message.getValue()).doesNotStartWith("{"); - assertThat(new BCryptPasswordEncoder().matches("boot", this.message.getValue())).isTrue(); + then(this.log).should().info(assertArg((message) -> { + assertThat(message).doesNotStartWith("{"); + assertThat(new BCryptPasswordEncoder().matches("boot", message)).isTrue(); + })); assertThat(status).isEqualTo(ExitStatus.OK); } @@ -81,10 +78,10 @@ class EncodePasswordCommandTests { void encodeWithPbkdf2ShouldUsePbkdf2() throws Exception { EncodePasswordCommand command = new EncodePasswordCommand(); ExitStatus status = command.run("-a", "pbkdf2", "boot"); - then(this.log).should().info(this.message.capture()); - assertThat(this.message.getValue()).doesNotStartWith("{"); - assertThat(Pbkdf2PasswordEncoder.defaultsForSpringSecurity_v5_8().matches("boot", this.message.getValue())) - .isTrue(); + then(this.log).should().info(assertArg((message) -> { + assertThat(message).doesNotStartWith("{"); + assertThat(Pbkdf2PasswordEncoder.defaultsForSpringSecurity_v5_8().matches("boot", message)).isTrue(); + })); assertThat(status).isEqualTo(ExitStatus.OK); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentContributorsTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentContributorsTests.java index 72c6a9373b4..fc2462447d6 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentContributorsTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataEnvironmentContributorsTests.java @@ -28,7 +28,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.ArgumentCaptor; -import org.mockito.Captor; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.boot.DefaultBootstrapContext; @@ -77,9 +76,6 @@ class ConfigDataEnvironmentContributorsTests { private ConfigDataActivationContext activationContext; - @Captor - private ArgumentCaptor locationResolverContext; - @BeforeEach void setup() { this.environment = new MockEnvironment(); @@ -213,10 +209,12 @@ class ConfigDataEnvironmentContributorsTests { ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofInitialImport(LOCATION_1); ConfigDataEnvironmentContributors contributors = new ConfigDataEnvironmentContributors(this.logFactory, this.bootstrapContext, Arrays.asList(contributor)); + ArgumentCaptor locationResolverContext = ArgumentCaptor + .forClass(ConfigDataLocationResolverContext.class); contributors.withProcessedImports(this.importer, this.activationContext); then(this.importer).should() - .resolveAndLoad(any(), this.locationResolverContext.capture(), any(), eq(secondLocations)); - ConfigDataLocationResolverContext context = this.locationResolverContext.getValue(); + .resolveAndLoad(any(), locationResolverContext.capture(), any(), eq(secondLocations)); + ConfigDataLocationResolverContext context = locationResolverContext.getValue(); assertThat(context.getParent()).hasToString("a"); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/MapBinderTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/MapBinderTests.java index 7b86c969206..cb03bcebc45 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/MapBinderTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/bind/MapBinderTests.java @@ -27,7 +27,6 @@ import java.util.Properties; import java.util.stream.Collectors; import org.junit.jupiter.api.Test; -import org.mockito.ArgumentCaptor; import org.mockito.InOrder; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -49,6 +48,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.entry; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.assertArg; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isA; import static org.mockito.BDDMockito.given; @@ -348,11 +348,9 @@ class MapBinderTests { Bindable> target = STRING_ARRAY_MAP; this.binder.bind("foo", target, handler); InOrder ordered = inOrder(handler); - ArgumentCaptor array = ArgumentCaptor.forClass(String[].class); ordered.verify(handler) .onSuccess(eq(ConfigurationPropertyName.of("foo.bar")), eq(Bindable.of(String[].class)), any(), - array.capture()); - assertThat(array.getValue()).containsExactly("a", "b", "c"); + assertArg((array) -> assertThat((String[]) array).containsExactly("a", "b", "c"))); ordered.verify(handler).onSuccess(eq(ConfigurationPropertyName.of("foo")), eq(target), any(), isA(Map.class)); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/SpringProfileModelHandlerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/SpringProfileModelHandlerTests.java index ac871668389..e90dded78d6 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/SpringProfileModelHandlerTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/SpringProfileModelHandlerTests.java @@ -25,7 +25,7 @@ import ch.qos.logback.core.model.processor.ModelHandlerException; import ch.qos.logback.core.model.processor.ModelInterpretationContext; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.ArgumentCaptor; +import org.mockito.ArgumentMatchers; import org.springframework.core.env.Environment; import org.springframework.core.env.Profiles; @@ -59,14 +59,14 @@ class SpringProfileModelHandlerTests { SpringProfileModel model = new SpringProfileModel(); model.setName("dev"); this.action.handle(this.interpretationContext, model); - ArgumentCaptor profiles = ArgumentCaptor.forClass(Profiles.class); - then(this.environment).should().acceptsProfiles(profiles.capture()); - List profileNames = new ArrayList<>(); - profiles.getValue().matches((profile) -> { - profileNames.add(profile); - return false; - }); - assertThat(profileNames).containsExactly("dev"); + then(this.environment).should().acceptsProfiles(ArgumentMatchers.assertArg(((profiles) -> { + List profileNames = new ArrayList<>(); + profiles.matches((profile) -> { + profileNames.add(profile); + return false; + }); + assertThat(profileNames).containsExactly("dev"); + }))); } @Test @@ -74,14 +74,14 @@ class SpringProfileModelHandlerTests { SpringProfileModel model = new SpringProfileModel(); model.setName("dev,qa"); this.action.handle(this.interpretationContext, model); - ArgumentCaptor profiles = ArgumentCaptor.forClass(Profiles.class); - then(this.environment).should().acceptsProfiles(profiles.capture()); - List profileNames = new ArrayList<>(); - profiles.getValue().matches((profile) -> { - profileNames.add(profile); - return false; - }); - assertThat(profileNames).containsExactly("dev", "qa"); + then(this.environment).should().acceptsProfiles(ArgumentMatchers.assertArg(((profiles) -> { + List profileNames = new ArrayList<>(); + profiles.matches((profile) -> { + profileNames.add(profile); + return false; + }); + assertThat(profileNames).containsExactly("dev", "qa"); + }))); } @Test @@ -90,14 +90,14 @@ class SpringProfileModelHandlerTests { model.setName("${profile}"); this.context.putProperty("profile", "dev"); this.action.handle(this.interpretationContext, model); - ArgumentCaptor profiles = ArgumentCaptor.forClass(Profiles.class); - then(this.environment).should().acceptsProfiles(profiles.capture()); - List profileNames = new ArrayList<>(); - profiles.getValue().matches((profile) -> { - profileNames.add(profile); - return false; - }); - assertThat(profileNames).containsExactly("dev"); + then(this.environment).should().acceptsProfiles(ArgumentMatchers.assertArg(((profiles) -> { + List profileNames = new ArrayList<>(); + profiles.matches((profile) -> { + profileNames.add(profile); + return false; + }); + assertThat(profileNames).containsExactly("dev"); + }))); } @Test @@ -108,14 +108,14 @@ class SpringProfileModelHandlerTests { this.context.putProperty("profile1", "dev"); this.context.putProperty("profile2", "qa"); this.action.handle(this.interpretationContext, model); - ArgumentCaptor profiles = ArgumentCaptor.forClass(Profiles.class); - then(this.environment).should().acceptsProfiles(profiles.capture()); - List profileNames = new ArrayList<>(); - profiles.getValue().matches((profile) -> { - profileNames.add(profile); - return false; - }); - assertThat(profileNames).containsExactly("dev", "qa"); + then(this.environment).should().acceptsProfiles(ArgumentMatchers.assertArg(((profiles) -> { + List profileNames = new ArrayList<>(); + profiles.matches((profile) -> { + profileNames.add(profile); + return false; + }); + assertThat(profileNames).containsExactly("dev", "qa"); + }))); } }