Polish "Replace Mockito argument captors with assertArg"

Co-authored-by: Andy Wilkinson <wilkinsona@vmware.com>

See gh-35015
This commit is contained in:
Moritz Halbritter 2023-04-20 07:37:58 +01:00
parent 80ca37984a
commit 441ed30ee4
7 changed files with 149 additions and 153 deletions

View File

@ -25,7 +25,7 @@ import java.util.Map;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; 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.context.properties.bind.Binder;
import org.springframework.boot.docker.compose.core.RunningService; import org.springframework.boot.docker.compose.core.RunningService;
@ -85,12 +85,12 @@ class ServiceReadinessChecksTests {
void loadCanResolveArguments() { void loadCanResolveArguments() {
this.loader = spy(MockSpringFactoriesLoader.class); this.loader = spy(MockSpringFactoriesLoader.class);
createChecks(); createChecks();
ArgumentCaptor<ArgumentResolver> captor = ArgumentCaptor.forClass(ArgumentResolver.class); then(this.loader).should()
then(this.loader).should().load(eq(ServiceReadinessCheck.class), captor.capture()); .load(eq(ServiceReadinessCheck.class), ArgumentMatchers.<ArgumentResolver>assertArg((argumentResolver) -> {
ArgumentResolver argumentResolver = captor.getValue(); assertThat(argumentResolver.resolve(ClassLoader.class)).isEqualTo(this.classLoader);
assertThat(argumentResolver.resolve(ClassLoader.class)).isEqualTo(this.classLoader); assertThat(argumentResolver.resolve(Environment.class)).isEqualTo(this.environment);
assertThat(argumentResolver.resolve(Environment.class)).isEqualTo(this.environment); assertThat(argumentResolver.resolve(Binder.class)).isEqualTo(this.binder);
assertThat(argumentResolver.resolve(Binder.class)).isEqualTo(this.binder); }));
} }
@Test @Test

View File

@ -21,10 +21,9 @@ import java.util.Map;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentMatchers;
import org.testcontainers.containers.PostgreSQLContainer; 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.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails; import org.springframework.boot.autoconfigure.jdbc.JdbcConnectionDetails;
@ -83,12 +82,12 @@ class ServiceConnectionContextCustomizerTests {
given(this.factories.getConnectionDetails(this.source, true)) given(this.factories.getConnectionDetails(this.source, true))
.willReturn(Map.of(JdbcConnectionDetails.class, connectionDetails)); .willReturn(Map.of(JdbcConnectionDetails.class, connectionDetails));
customizer.customizeContext(context, mergedConfig); customizer.customizeContext(context, mergedConfig);
ArgumentCaptor<BeanDefinition> beanDefinitionCaptor = ArgumentCaptor.forClass(BeanDefinition.class);
then(beanFactory).should() then(beanFactory).should()
.registerBeanDefinition(eq("testJdbcConnectionDetailsForTest"), beanDefinitionCaptor.capture()); .registerBeanDefinition(eq("testJdbcConnectionDetailsForTest"),
RootBeanDefinition beanDefinition = (RootBeanDefinition) beanDefinitionCaptor.getValue(); ArgumentMatchers.<RootBeanDefinition>assertArg((beanDefinition) -> {
assertThat(beanDefinition.getInstanceSupplier().get()).isSameAs(connectionDetails); assertThat(beanDefinition.getInstanceSupplier().get()).isSameAs(connectionDetails);
assertThat(beanDefinition.getBeanClass()).isEqualTo(TestJdbcConnectionDetails.class); assertThat(beanDefinition.getBeanClass()).isEqualTo(TestJdbcConnectionDetails.class);
}));
} }
@Test @Test

View File

@ -33,11 +33,10 @@ import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.HttpEntity; import org.apache.hc.core5.http.HttpEntity;
import org.apache.hc.core5.http.HttpHeaders; import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.HttpHost; import org.apache.hc.core5.http.HttpHost;
import org.assertj.core.api.ThrowingConsumer;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
@ -78,9 +77,6 @@ class HttpClientTransportTests {
@Mock @Mock
private InputStream content; private InputStream content;
@Captor
private ArgumentCaptor<HttpUriRequest> requestCaptor;
private HttpClientTransport http; private HttpClientTransport http;
private URI uri; private URI uri;
@ -117,13 +113,14 @@ class HttpClientTransportTests {
given(this.entity.getContent()).willReturn(this.content); given(this.entity.getContent()).willReturn(this.content);
given(this.response.getCode()).willReturn(200); given(this.response.getCode()).willReturn(200);
Response response = this.http.post(this.uri); Response response = this.http.post(this.uri);
then(this.client).should().executeOpen(any(HttpHost.class), this.requestCaptor.capture(), isNull()); then(this.client).should()
HttpUriRequest request = this.requestCaptor.getValue(); .executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer<HttpPost>) (request) -> {
assertThat(request).isInstanceOf(HttpPost.class); assertThat(request).isInstanceOf(HttpPost.class);
assertThat(request.getUri()).isEqualTo(this.uri); assertThat(request.getUri()).isEqualTo(this.uri);
assertThat(request.getFirstHeader(HttpHeaders.CONTENT_TYPE)).isNull(); assertThat(request.getFirstHeader(HttpHeaders.CONTENT_TYPE)).isNull();
assertThat(request.getFirstHeader(HttpClientTransport.REGISTRY_AUTH_HEADER)).isNull(); assertThat(request.getFirstHeader(HttpClientTransport.REGISTRY_AUTH_HEADER)).isNull();
assertThat(response.getContent()).isSameAs(this.content); assertThat(response.getContent()).isSameAs(this.content);
}), isNull());
} }
@Test @Test
@ -132,13 +129,15 @@ class HttpClientTransportTests {
given(this.entity.getContent()).willReturn(this.content); given(this.entity.getContent()).willReturn(this.content);
given(this.response.getCode()).willReturn(200); given(this.response.getCode()).willReturn(200);
Response response = this.http.post(this.uri, "auth token"); Response response = this.http.post(this.uri, "auth token");
then(this.client).should().executeOpen(any(HttpHost.class), this.requestCaptor.capture(), isNull()); then(this.client).should()
HttpUriRequest request = this.requestCaptor.getValue(); .executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer<HttpPost>) (request) -> {
assertThat(request).isInstanceOf(HttpPost.class); assertThat(request).isInstanceOf(HttpPost.class);
assertThat(request.getUri()).isEqualTo(this.uri); assertThat(request.getUri()).isEqualTo(this.uri);
assertThat(request.getFirstHeader(HttpHeaders.CONTENT_TYPE)).isNull(); assertThat(request.getFirstHeader(HttpHeaders.CONTENT_TYPE)).isNull();
assertThat(request.getFirstHeader(HttpClientTransport.REGISTRY_AUTH_HEADER).getValue()).isEqualTo("auth token"); assertThat(request.getFirstHeader(HttpClientTransport.REGISTRY_AUTH_HEADER).getValue())
assertThat(response.getContent()).isSameAs(this.content); .isEqualTo("auth token");
assertThat(response.getContent()).isSameAs(this.content);
}), isNull());
} }
@Test @Test
@ -147,13 +146,14 @@ class HttpClientTransportTests {
given(this.entity.getContent()).willReturn(this.content); given(this.entity.getContent()).willReturn(this.content);
given(this.response.getCode()).willReturn(200); given(this.response.getCode()).willReturn(200);
Response response = this.http.post(this.uri, ""); Response response = this.http.post(this.uri, "");
then(this.client).should().executeOpen(any(HttpHost.class), this.requestCaptor.capture(), isNull()); then(this.client).should()
HttpUriRequest request = this.requestCaptor.getValue(); .executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer<HttpPost>) (request) -> {
assertThat(request).isInstanceOf(HttpPost.class); assertThat(request).isInstanceOf(HttpPost.class);
assertThat(request.getUri()).isEqualTo(this.uri); assertThat(request.getUri()).isEqualTo(this.uri);
assertThat(request.getFirstHeader(HttpHeaders.CONTENT_TYPE)).isNull(); assertThat(request.getFirstHeader(HttpHeaders.CONTENT_TYPE)).isNull();
assertThat(request.getFirstHeader(HttpClientTransport.REGISTRY_AUTH_HEADER)).isNull(); assertThat(request.getFirstHeader(HttpClientTransport.REGISTRY_AUTH_HEADER)).isNull();
assertThat(response.getContent()).isSameAs(this.content); assertThat(response.getContent()).isSameAs(this.content);
}), isNull());
} }
@Test @Test
@ -164,18 +164,19 @@ class HttpClientTransportTests {
given(this.response.getCode()).willReturn(200); given(this.response.getCode()).willReturn(200);
Response response = this.http.post(this.uri, APPLICATION_JSON, Response response = this.http.post(this.uri, APPLICATION_JSON,
(out) -> StreamUtils.copy(content, StandardCharsets.UTF_8, out)); (out) -> StreamUtils.copy(content, StandardCharsets.UTF_8, out));
then(this.client).should().executeOpen(any(HttpHost.class), this.requestCaptor.capture(), isNull()); then(this.client).should()
HttpUriRequest request = this.requestCaptor.getValue(); .executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer<HttpPost>) (request) -> {
HttpEntity entity = request.getEntity(); HttpEntity entity = request.getEntity();
assertThat(request).isInstanceOf(HttpPost.class); assertThat(request).isInstanceOf(HttpPost.class);
assertThat(request.getUri()).isEqualTo(this.uri); assertThat(request.getUri()).isEqualTo(this.uri);
assertThat(entity.isRepeatable()).isFalse(); assertThat(entity.isRepeatable()).isFalse();
assertThat(entity.getContentLength()).isEqualTo(content.length()); assertThat(entity.getContentLength()).isEqualTo(content.length());
assertThat(entity.getContentType()).isEqualTo(APPLICATION_JSON); assertThat(entity.getContentType()).isEqualTo(APPLICATION_JSON);
assertThat(entity.isStreaming()).isTrue(); assertThat(entity.isStreaming()).isTrue();
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent); assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent);
assertThat(writeToString(entity)).isEqualTo(content); assertThat(writeToString(entity)).isEqualTo(content);
assertThat(response.getContent()).isSameAs(this.content); assertThat(response.getContent()).isSameAs(this.content);
}), isNull());
} }
@Test @Test
@ -186,18 +187,19 @@ class HttpClientTransportTests {
given(this.response.getCode()).willReturn(200); given(this.response.getCode()).willReturn(200);
Response response = this.http.post(this.uri, APPLICATION_X_TAR, Response response = this.http.post(this.uri, APPLICATION_X_TAR,
(out) -> StreamUtils.copy(content, StandardCharsets.UTF_8, out)); (out) -> StreamUtils.copy(content, StandardCharsets.UTF_8, out));
then(this.client).should().executeOpen(any(HttpHost.class), this.requestCaptor.capture(), isNull()); then(this.client).should()
HttpUriRequest request = this.requestCaptor.getValue(); .executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer<HttpPost>) (request) -> {
HttpEntity entity = request.getEntity(); HttpEntity entity = request.getEntity();
assertThat(request).isInstanceOf(HttpPost.class); assertThat(request).isInstanceOf(HttpPost.class);
assertThat(request.getUri()).isEqualTo(this.uri); assertThat(request.getUri()).isEqualTo(this.uri);
assertThat(entity.isRepeatable()).isFalse(); assertThat(entity.isRepeatable()).isFalse();
assertThat(entity.getContentLength()).isEqualTo(-1); assertThat(entity.getContentLength()).isEqualTo(-1);
assertThat(entity.getContentType()).isEqualTo(APPLICATION_X_TAR); assertThat(entity.getContentType()).isEqualTo(APPLICATION_X_TAR);
assertThat(entity.isStreaming()).isTrue(); assertThat(entity.isStreaming()).isTrue();
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent); assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent);
assertThat(writeToString(entity)).isEqualTo(content); assertThat(writeToString(entity)).isEqualTo(content);
assertThat(response.getContent()).isSameAs(this.content); assertThat(response.getContent()).isSameAs(this.content);
}), isNull());
} }
@Test @Test
@ -208,18 +210,18 @@ class HttpClientTransportTests {
given(this.response.getCode()).willReturn(200); given(this.response.getCode()).willReturn(200);
Response response = this.http.put(this.uri, APPLICATION_JSON, Response response = this.http.put(this.uri, APPLICATION_JSON,
(out) -> StreamUtils.copy(content, StandardCharsets.UTF_8, out)); (out) -> StreamUtils.copy(content, StandardCharsets.UTF_8, out));
then(this.client).should().executeOpen(any(HttpHost.class), this.requestCaptor.capture(), isNull()); then(this.client).should().executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer<HttpPut>) (request) -> {
HttpUriRequest request = this.requestCaptor.getValue(); HttpEntity entity = request.getEntity();
HttpEntity entity = request.getEntity(); assertThat(request).isInstanceOf(HttpPut.class);
assertThat(request).isInstanceOf(HttpPut.class); assertThat(request.getUri()).isEqualTo(this.uri);
assertThat(request.getUri()).isEqualTo(this.uri); assertThat(entity.isRepeatable()).isFalse();
assertThat(entity.isRepeatable()).isFalse(); assertThat(entity.getContentLength()).isEqualTo(content.length());
assertThat(entity.getContentLength()).isEqualTo(content.length()); assertThat(entity.getContentType()).isEqualTo(APPLICATION_JSON);
assertThat(entity.getContentType()).isEqualTo(APPLICATION_JSON); assertThat(entity.isStreaming()).isTrue();
assertThat(entity.isStreaming()).isTrue(); assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent);
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent); assertThat(writeToString(entity)).isEqualTo(content);
assertThat(writeToString(entity)).isEqualTo(content); assertThat(response.getContent()).isSameAs(this.content);
assertThat(response.getContent()).isSameAs(this.content); }), isNull());
} }
@Test @Test
@ -230,18 +232,18 @@ class HttpClientTransportTests {
given(this.response.getCode()).willReturn(200); given(this.response.getCode()).willReturn(200);
Response response = this.http.put(this.uri, APPLICATION_X_TAR, Response response = this.http.put(this.uri, APPLICATION_X_TAR,
(out) -> StreamUtils.copy(content, StandardCharsets.UTF_8, out)); (out) -> StreamUtils.copy(content, StandardCharsets.UTF_8, out));
then(this.client).should().executeOpen(any(HttpHost.class), this.requestCaptor.capture(), isNull()); then(this.client).should().executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer<HttpPut>) (request) -> {
HttpUriRequest request = this.requestCaptor.getValue(); HttpEntity entity = request.getEntity();
HttpEntity entity = request.getEntity(); assertThat(request).isInstanceOf(HttpPut.class);
assertThat(request).isInstanceOf(HttpPut.class); assertThat(request.getUri()).isEqualTo(this.uri);
assertThat(request.getUri()).isEqualTo(this.uri); assertThat(entity.isRepeatable()).isFalse();
assertThat(entity.isRepeatable()).isFalse(); assertThat(entity.getContentLength()).isEqualTo(-1);
assertThat(entity.getContentLength()).isEqualTo(-1); assertThat(entity.getContentType()).isEqualTo(APPLICATION_X_TAR);
assertThat(entity.getContentType()).isEqualTo(APPLICATION_X_TAR); assertThat(entity.isStreaming()).isTrue();
assertThat(entity.isStreaming()).isTrue(); assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent);
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent); assertThat(writeToString(entity)).isEqualTo(content);
assertThat(writeToString(entity)).isEqualTo(content); assertThat(response.getContent()).isSameAs(this.content);
assertThat(response.getContent()).isSameAs(this.content); }), isNull());
} }
@Test @Test
@ -250,12 +252,14 @@ class HttpClientTransportTests {
given(this.entity.getContent()).willReturn(this.content); given(this.entity.getContent()).willReturn(this.content);
given(this.response.getCode()).willReturn(200); given(this.response.getCode()).willReturn(200);
Response response = this.http.delete(this.uri); Response response = this.http.delete(this.uri);
then(this.client).should().executeOpen(any(HttpHost.class), this.requestCaptor.capture(), isNull());
HttpUriRequest request = this.requestCaptor.getValue(); then(this.client).should()
assertThat(request).isInstanceOf(HttpDelete.class); .executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer<HttpDelete>) (request) -> {
assertThat(request.getUri()).isEqualTo(this.uri); assertThat(request).isInstanceOf(HttpDelete.class);
assertThat(request.getFirstHeader(HttpHeaders.CONTENT_TYPE)).isNull(); assertThat(request.getUri()).isEqualTo(this.uri);
assertThat(response.getContent()).isSameAs(this.content); assertThat(request.getFirstHeader(HttpHeaders.CONTENT_TYPE)).isNull();
assertThat(response.getContent()).isSameAs(this.content);
}), isNull());
} }
@Test @Test

View File

@ -20,8 +20,6 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.boot.cli.command.status.ExitStatus; 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 org.springframework.security.crypto.password.Pbkdf2PasswordEncoder;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.assertArg;
import static org.mockito.BDDMockito.then; import static org.mockito.BDDMockito.then;
/** /**
@ -43,9 +42,6 @@ class EncodePasswordCommandTests {
private MockLog log; private MockLog log;
@Captor
private ArgumentCaptor<String> message;
@BeforeEach @BeforeEach
void setup() { void setup() {
this.log = MockLog.attach(); this.log = MockLog.attach();
@ -60,10 +56,10 @@ class EncodePasswordCommandTests {
void encodeWithNoAlgorithmShouldUseBcrypt() throws Exception { void encodeWithNoAlgorithmShouldUseBcrypt() throws Exception {
EncodePasswordCommand command = new EncodePasswordCommand(); EncodePasswordCommand command = new EncodePasswordCommand();
ExitStatus status = command.run("boot"); ExitStatus status = command.run("boot");
then(this.log).should().info(this.message.capture()); then(this.log).should().info(assertArg((message) -> {
assertThat(this.message.getValue()).startsWith("{bcrypt}"); assertThat(message).startsWith("{bcrypt}");
assertThat(PasswordEncoderFactories.createDelegatingPasswordEncoder().matches("boot", this.message.getValue())) assertThat(PasswordEncoderFactories.createDelegatingPasswordEncoder().matches("boot", message)).isTrue();
.isTrue(); }));
assertThat(status).isEqualTo(ExitStatus.OK); assertThat(status).isEqualTo(ExitStatus.OK);
} }
@ -71,9 +67,10 @@ class EncodePasswordCommandTests {
void encodeWithBCryptShouldUseBCrypt() throws Exception { void encodeWithBCryptShouldUseBCrypt() throws Exception {
EncodePasswordCommand command = new EncodePasswordCommand(); EncodePasswordCommand command = new EncodePasswordCommand();
ExitStatus status = command.run("-a", "bcrypt", "boot"); ExitStatus status = command.run("-a", "bcrypt", "boot");
then(this.log).should().info(this.message.capture()); then(this.log).should().info(assertArg((message) -> {
assertThat(this.message.getValue()).doesNotStartWith("{"); assertThat(message).doesNotStartWith("{");
assertThat(new BCryptPasswordEncoder().matches("boot", this.message.getValue())).isTrue(); assertThat(new BCryptPasswordEncoder().matches("boot", message)).isTrue();
}));
assertThat(status).isEqualTo(ExitStatus.OK); assertThat(status).isEqualTo(ExitStatus.OK);
} }
@ -81,10 +78,10 @@ class EncodePasswordCommandTests {
void encodeWithPbkdf2ShouldUsePbkdf2() throws Exception { void encodeWithPbkdf2ShouldUsePbkdf2() throws Exception {
EncodePasswordCommand command = new EncodePasswordCommand(); EncodePasswordCommand command = new EncodePasswordCommand();
ExitStatus status = command.run("-a", "pbkdf2", "boot"); ExitStatus status = command.run("-a", "pbkdf2", "boot");
then(this.log).should().info(this.message.capture()); then(this.log).should().info(assertArg((message) -> {
assertThat(this.message.getValue()).doesNotStartWith("{"); assertThat(message).doesNotStartWith("{");
assertThat(Pbkdf2PasswordEncoder.defaultsForSpringSecurity_v5_8().matches("boot", this.message.getValue())) assertThat(Pbkdf2PasswordEncoder.defaultsForSpringSecurity_v5_8().matches("boot", message)).isTrue();
.isTrue(); }));
assertThat(status).isEqualTo(ExitStatus.OK); assertThat(status).isEqualTo(ExitStatus.OK);
} }

View File

@ -28,7 +28,6 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.boot.DefaultBootstrapContext; import org.springframework.boot.DefaultBootstrapContext;
@ -77,9 +76,6 @@ class ConfigDataEnvironmentContributorsTests {
private ConfigDataActivationContext activationContext; private ConfigDataActivationContext activationContext;
@Captor
private ArgumentCaptor<ConfigDataLocationResolverContext> locationResolverContext;
@BeforeEach @BeforeEach
void setup() { void setup() {
this.environment = new MockEnvironment(); this.environment = new MockEnvironment();
@ -213,10 +209,12 @@ class ConfigDataEnvironmentContributorsTests {
ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofInitialImport(LOCATION_1); ConfigDataEnvironmentContributor contributor = ConfigDataEnvironmentContributor.ofInitialImport(LOCATION_1);
ConfigDataEnvironmentContributors contributors = new ConfigDataEnvironmentContributors(this.logFactory, ConfigDataEnvironmentContributors contributors = new ConfigDataEnvironmentContributors(this.logFactory,
this.bootstrapContext, Arrays.asList(contributor)); this.bootstrapContext, Arrays.asList(contributor));
ArgumentCaptor<ConfigDataLocationResolverContext> locationResolverContext = ArgumentCaptor
.forClass(ConfigDataLocationResolverContext.class);
contributors.withProcessedImports(this.importer, this.activationContext); contributors.withProcessedImports(this.importer, this.activationContext);
then(this.importer).should() then(this.importer).should()
.resolveAndLoad(any(), this.locationResolverContext.capture(), any(), eq(secondLocations)); .resolveAndLoad(any(), locationResolverContext.capture(), any(), eq(secondLocations));
ConfigDataLocationResolverContext context = this.locationResolverContext.getValue(); ConfigDataLocationResolverContext context = locationResolverContext.getValue();
assertThat(context.getParent()).hasToString("a"); assertThat(context.getParent()).hasToString("a");
} }

View File

@ -27,7 +27,6 @@ import java.util.Properties;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InOrder; import org.mockito.InOrder;
import org.mockito.invocation.InvocationOnMock; import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer; 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.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.entry; import static org.assertj.core.api.Assertions.entry;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.assertArg;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isA; import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
@ -348,11 +348,9 @@ class MapBinderTests {
Bindable<Map<String, String[]>> target = STRING_ARRAY_MAP; Bindable<Map<String, String[]>> target = STRING_ARRAY_MAP;
this.binder.bind("foo", target, handler); this.binder.bind("foo", target, handler);
InOrder ordered = inOrder(handler); InOrder ordered = inOrder(handler);
ArgumentCaptor<String[]> array = ArgumentCaptor.forClass(String[].class);
ordered.verify(handler) ordered.verify(handler)
.onSuccess(eq(ConfigurationPropertyName.of("foo.bar")), eq(Bindable.of(String[].class)), any(), .onSuccess(eq(ConfigurationPropertyName.of("foo.bar")), eq(Bindable.of(String[].class)), any(),
array.capture()); assertArg((array) -> assertThat((String[]) array).containsExactly("a", "b", "c")));
assertThat(array.getValue()).containsExactly("a", "b", "c");
ordered.verify(handler).onSuccess(eq(ConfigurationPropertyName.of("foo")), eq(target), any(), isA(Map.class)); ordered.verify(handler).onSuccess(eq(ConfigurationPropertyName.of("foo")), eq(target), any(), isA(Map.class));
} }

View File

@ -25,7 +25,7 @@ import ch.qos.logback.core.model.processor.ModelHandlerException;
import ch.qos.logback.core.model.processor.ModelInterpretationContext; import ch.qos.logback.core.model.processor.ModelInterpretationContext;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; 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.Environment;
import org.springframework.core.env.Profiles; import org.springframework.core.env.Profiles;
@ -59,14 +59,14 @@ class SpringProfileModelHandlerTests {
SpringProfileModel model = new SpringProfileModel(); SpringProfileModel model = new SpringProfileModel();
model.setName("dev"); model.setName("dev");
this.action.handle(this.interpretationContext, model); this.action.handle(this.interpretationContext, model);
ArgumentCaptor<Profiles> profiles = ArgumentCaptor.forClass(Profiles.class); then(this.environment).should().acceptsProfiles(ArgumentMatchers.<Profiles>assertArg(((profiles) -> {
then(this.environment).should().acceptsProfiles(profiles.capture()); List<String> profileNames = new ArrayList<>();
List<String> profileNames = new ArrayList<>(); profiles.matches((profile) -> {
profiles.getValue().matches((profile) -> { profileNames.add(profile);
profileNames.add(profile); return false;
return false; });
}); assertThat(profileNames).containsExactly("dev");
assertThat(profileNames).containsExactly("dev"); })));
} }
@Test @Test
@ -74,14 +74,14 @@ class SpringProfileModelHandlerTests {
SpringProfileModel model = new SpringProfileModel(); SpringProfileModel model = new SpringProfileModel();
model.setName("dev,qa"); model.setName("dev,qa");
this.action.handle(this.interpretationContext, model); this.action.handle(this.interpretationContext, model);
ArgumentCaptor<Profiles> profiles = ArgumentCaptor.forClass(Profiles.class); then(this.environment).should().acceptsProfiles(ArgumentMatchers.<Profiles>assertArg(((profiles) -> {
then(this.environment).should().acceptsProfiles(profiles.capture()); List<String> profileNames = new ArrayList<>();
List<String> profileNames = new ArrayList<>(); profiles.matches((profile) -> {
profiles.getValue().matches((profile) -> { profileNames.add(profile);
profileNames.add(profile); return false;
return false; });
}); assertThat(profileNames).containsExactly("dev", "qa");
assertThat(profileNames).containsExactly("dev", "qa"); })));
} }
@Test @Test
@ -90,14 +90,14 @@ class SpringProfileModelHandlerTests {
model.setName("${profile}"); model.setName("${profile}");
this.context.putProperty("profile", "dev"); this.context.putProperty("profile", "dev");
this.action.handle(this.interpretationContext, model); this.action.handle(this.interpretationContext, model);
ArgumentCaptor<Profiles> profiles = ArgumentCaptor.forClass(Profiles.class); then(this.environment).should().acceptsProfiles(ArgumentMatchers.<Profiles>assertArg(((profiles) -> {
then(this.environment).should().acceptsProfiles(profiles.capture()); List<String> profileNames = new ArrayList<>();
List<String> profileNames = new ArrayList<>(); profiles.matches((profile) -> {
profiles.getValue().matches((profile) -> { profileNames.add(profile);
profileNames.add(profile); return false;
return false; });
}); assertThat(profileNames).containsExactly("dev");
assertThat(profileNames).containsExactly("dev"); })));
} }
@Test @Test
@ -108,14 +108,14 @@ class SpringProfileModelHandlerTests {
this.context.putProperty("profile1", "dev"); this.context.putProperty("profile1", "dev");
this.context.putProperty("profile2", "qa"); this.context.putProperty("profile2", "qa");
this.action.handle(this.interpretationContext, model); this.action.handle(this.interpretationContext, model);
ArgumentCaptor<Profiles> profiles = ArgumentCaptor.forClass(Profiles.class); then(this.environment).should().acceptsProfiles(ArgumentMatchers.<Profiles>assertArg(((profiles) -> {
then(this.environment).should().acceptsProfiles(profiles.capture()); List<String> profileNames = new ArrayList<>();
List<String> profileNames = new ArrayList<>(); profiles.matches((profile) -> {
profiles.getValue().matches((profile) -> { profileNames.add(profile);
profileNames.add(profile); return false;
return false; });
}); assertThat(profileNames).containsExactly("dev", "qa");
assertThat(profileNames).containsExactly("dev", "qa"); })));
} }
} }