Polish "Replace Mockito argument captors with assertArg"

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

See gh-35015
pull/35333/head
Moritz Halbritter 2 years ago
parent 80ca37984a
commit 441ed30ee4

@ -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<ArgumentResolver> captor = ArgumentCaptor.forClass(ArgumentResolver.class);
then(this.loader).should().load(eq(ServiceReadinessCheck.class), captor.capture());
ArgumentResolver argumentResolver = captor.getValue();
then(this.loader).should()
.load(eq(ServiceReadinessCheck.class), ArgumentMatchers.<ArgumentResolver>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

@ -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<BeanDefinition> beanDefinitionCaptor = ArgumentCaptor.forClass(BeanDefinition.class);
then(beanFactory).should()
.registerBeanDefinition(eq("testJdbcConnectionDetailsForTest"), beanDefinitionCaptor.capture());
RootBeanDefinition beanDefinition = (RootBeanDefinition) beanDefinitionCaptor.getValue();
.registerBeanDefinition(eq("testJdbcConnectionDetailsForTest"),
ArgumentMatchers.<RootBeanDefinition>assertArg((beanDefinition) -> {
assertThat(beanDefinition.getInstanceSupplier().get()).isSameAs(connectionDetails);
assertThat(beanDefinition.getBeanClass()).isEqualTo(TestJdbcConnectionDetails.class);
}));
}
@Test

@ -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<HttpUriRequest> 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();
then(this.client).should()
.executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer<HttpPost>) (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();
then(this.client).should()
.executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer<HttpPost>) (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(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();
then(this.client).should()
.executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer<HttpPost>) (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,8 +164,8 @@ 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();
then(this.client).should()
.executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer<HttpPost>) (request) -> {
HttpEntity entity = request.getEntity();
assertThat(request).isInstanceOf(HttpPost.class);
assertThat(request.getUri()).isEqualTo(this.uri);
@ -176,6 +176,7 @@ class HttpClientTransportTests {
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent);
assertThat(writeToString(entity)).isEqualTo(content);
assertThat(response.getContent()).isSameAs(this.content);
}), isNull());
}
@Test
@ -186,8 +187,8 @@ 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();
then(this.client).should()
.executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer<HttpPost>) (request) -> {
HttpEntity entity = request.getEntity();
assertThat(request).isInstanceOf(HttpPost.class);
assertThat(request.getUri()).isEqualTo(this.uri);
@ -198,6 +199,7 @@ class HttpClientTransportTests {
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent);
assertThat(writeToString(entity)).isEqualTo(content);
assertThat(response.getContent()).isSameAs(this.content);
}), isNull());
}
@Test
@ -208,8 +210,7 @@ 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();
then(this.client).should().executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer<HttpPut>) (request) -> {
HttpEntity entity = request.getEntity();
assertThat(request).isInstanceOf(HttpPut.class);
assertThat(request.getUri()).isEqualTo(this.uri);
@ -220,6 +221,7 @@ class HttpClientTransportTests {
assertThatExceptionOfType(UnsupportedOperationException.class).isThrownBy(entity::getContent);
assertThat(writeToString(entity)).isEqualTo(content);
assertThat(response.getContent()).isSameAs(this.content);
}), isNull());
}
@Test
@ -230,8 +232,7 @@ 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();
then(this.client).should().executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer<HttpPut>) (request) -> {
HttpEntity entity = request.getEntity();
assertThat(request).isInstanceOf(HttpPut.class);
assertThat(request.getUri()).isEqualTo(this.uri);
@ -242,6 +243,7 @@ class HttpClientTransportTests {
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();
then(this.client).should()
.executeOpen(any(HttpHost.class), assertArg((ThrowingConsumer<HttpDelete>) (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

@ -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<String> 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);
}

@ -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<ConfigDataLocationResolverContext> 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<ConfigDataLocationResolverContext> 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");
}

@ -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<Map<String, String[]>> target = STRING_ARRAY_MAP;
this.binder.bind("foo", target, handler);
InOrder ordered = inOrder(handler);
ArgumentCaptor<String[]> 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));
}

@ -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> profiles = ArgumentCaptor.forClass(Profiles.class);
then(this.environment).should().acceptsProfiles(profiles.capture());
then(this.environment).should().acceptsProfiles(ArgumentMatchers.<Profiles>assertArg(((profiles) -> {
List<String> profileNames = new ArrayList<>();
profiles.getValue().matches((profile) -> {
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> profiles = ArgumentCaptor.forClass(Profiles.class);
then(this.environment).should().acceptsProfiles(profiles.capture());
then(this.environment).should().acceptsProfiles(ArgumentMatchers.<Profiles>assertArg(((profiles) -> {
List<String> profileNames = new ArrayList<>();
profiles.getValue().matches((profile) -> {
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> profiles = ArgumentCaptor.forClass(Profiles.class);
then(this.environment).should().acceptsProfiles(profiles.capture());
then(this.environment).should().acceptsProfiles(ArgumentMatchers.<Profiles>assertArg(((profiles) -> {
List<String> profileNames = new ArrayList<>();
profiles.getValue().matches((profile) -> {
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> profiles = ArgumentCaptor.forClass(Profiles.class);
then(this.environment).should().acceptsProfiles(profiles.capture());
then(this.environment).should().acceptsProfiles(ArgumentMatchers.<Profiles>assertArg(((profiles) -> {
List<String> profileNames = new ArrayList<>();
profiles.getValue().matches((profile) -> {
profiles.matches((profile) -> {
profileNames.add(profile);
return false;
});
assertThat(profileNames).containsExactly("dev", "qa");
})));
}
}

Loading…
Cancel
Save