Replace Mockito argument captors with assertArg

See gh-35015
This commit is contained in:
Marc Leroux
2023-04-16 20:49:34 -04:00
committed by Moritz Halbritter
parent 4d14d0e437
commit b61834c92d
27 changed files with 186 additions and 250 deletions

View File

@@ -17,7 +17,6 @@
package org.springframework.boot.test.mock.mockito;
import java.io.InputStream;
import java.lang.reflect.Field;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -32,6 +31,7 @@ import org.springframework.test.context.support.DependencyInjectionTestExecution
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.assertArg;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
@@ -53,9 +53,6 @@ class MockitoTestExecutionListenerTests {
@Mock
private MockitoPostProcessor postProcessor;
@Captor
private ArgumentCaptor<Field> fieldCaptor;
@Test
void prepareTestInstanceShouldInitMockitoAnnotations() throws Exception {
WithMockitoAnnotations instance = new WithMockitoAnnotations();
@@ -71,8 +68,9 @@ class MockitoTestExecutionListenerTests {
TestContext testContext = mockTestContext(instance);
given(testContext.getApplicationContext()).willReturn(this.applicationContext);
this.listener.prepareTestInstance(testContext);
then(this.postProcessor).should().inject(this.fieldCaptor.capture(), eq(instance), any(MockDefinition.class));
assertThat(this.fieldCaptor.getValue().getName()).isEqualTo("mockBean");
then(this.postProcessor).should()
.inject(assertArg((field) -> assertThat(field.getName()).isEqualTo("mockBean")), eq(instance),
any(MockDefinition.class));
}
@Test
@@ -90,8 +88,9 @@ class MockitoTestExecutionListenerTests {
given(mockTestContext.getAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE))
.willReturn(Boolean.TRUE);
this.listener.beforeTestMethod(mockTestContext);
then(this.postProcessor).should().inject(this.fieldCaptor.capture(), eq(instance), any(MockDefinition.class));
assertThat(this.fieldCaptor.getValue().getName()).isEqualTo("mockBean");
then(this.postProcessor).should()
.inject(assertArg((field) -> assertThat(field.getName()).isEqualTo("mockBean")), eq(instance),
any(MockDefinition.class));
}
@SuppressWarnings({ "unchecked", "rawtypes" })

View File

@@ -22,19 +22,17 @@ import java.lang.reflect.Field;
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;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.DependencyDescriptor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ReflectionUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.assertArg;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.then;
@@ -49,9 +47,6 @@ class QualifierDefinitionTests {
@Mock
private ConfigurableListableBeanFactory beanFactory;
@Captor
private ArgumentCaptor<DependencyDescriptor> descriptorCaptor;
@Test
void forElementFieldIsNullShouldReturnNull() {
assertThat(QualifierDefinition.forElement((Field) null)).isNull();
@@ -81,8 +76,9 @@ class QualifierDefinitionTests {
Field field = ReflectionUtils.findField(ConfigA.class, "directQualifier");
QualifierDefinition qualifierDefinition = QualifierDefinition.forElement(field);
qualifierDefinition.matches(this.beanFactory, "bean");
then(this.beanFactory).should().isAutowireCandidate(eq("bean"), this.descriptorCaptor.capture());
assertThat(this.descriptorCaptor.getValue().getAnnotatedElement()).isEqualTo(field);
then(this.beanFactory).should()
.isAutowireCandidate(eq("bean"), assertArg(
(dependencyDescriptor) -> assertThat(dependencyDescriptor.getAnnotatedElement()).isEqualTo(field)));
}
@Test

View File

@@ -21,8 +21,6 @@ import java.net.URI;
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;
@@ -39,6 +37,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.assertArg;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;
@@ -60,9 +59,6 @@ class RootUriRequestExpectationManagerTests {
private RootUriRequestExpectationManager manager;
@Captor
private ArgumentCaptor<ClientHttpRequest> requestCaptor;
@BeforeEach
void setup() {
this.manager = new RootUriRequestExpectationManager(this.uri, this.delegate);
@@ -101,10 +97,14 @@ class RootUriRequestExpectationManagerTests {
ClientHttpRequest request = mock(ClientHttpRequest.class);
given(request.getURI()).willReturn(new URI(this.uri + "/hello"));
this.manager.validateRequest(request);
then(this.delegate).should().validateRequest(this.requestCaptor.capture());
HttpRequestWrapper actual = (HttpRequestWrapper) this.requestCaptor.getValue();
assertThat(actual.getRequest()).isSameAs(request);
assertThat(actual.getURI()).isEqualTo(new URI("/hello"));
URI expectedURI = new URI("/hello");
then(this.delegate).should()
.validateRequest(assertArg((actual) -> assertThat(actual).isInstanceOfSatisfying(HttpRequestWrapper.class,
(requestWrapper) -> {
assertThat(requestWrapper.getRequest()).isSameAs(request);
assertThat(requestWrapper.getURI()).isEqualTo(expectedURI);
})));
}
@Test

View File

@@ -22,12 +22,9 @@ import java.net.URL;
import com.gargoylesoftware.htmlunit.StringWebResponse;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebConnection;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.WebResponse;
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.mock.env.MockEnvironment;
@@ -35,6 +32,7 @@ import org.springframework.mock.env.MockEnvironment;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.assertArg;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.mock;
@@ -48,9 +46,6 @@ import static org.mockito.Mockito.mock;
@ExtendWith(MockitoExtension.class)
class LocalHostWebClientTests {
@Captor
private ArgumentCaptor<WebRequest> requestCaptor;
@Test
void createWhenEnvironmentIsNullWillThrowException() {
assertThatIllegalArgumentException().isThrownBy(() -> new LocalHostWebClient(null))
@@ -64,8 +59,9 @@ class LocalHostWebClientTests {
WebConnection connection = mockConnection();
client.setWebConnection(connection);
client.getPage("/test");
then(connection).should().getResponse(this.requestCaptor.capture());
assertThat(this.requestCaptor.getValue().getUrl()).isEqualTo(new URL("http://localhost:8080/test"));
URL expectedUrl = new URL("http://localhost:8080/test");
then(connection).should()
.getResponse(assertArg((request) -> assertThat(request.getUrl()).isEqualTo(expectedUrl)));
}
@Test
@@ -76,8 +72,9 @@ class LocalHostWebClientTests {
WebConnection connection = mockConnection();
client.setWebConnection(connection);
client.getPage("/test");
then(connection).should().getResponse(this.requestCaptor.capture());
assertThat(this.requestCaptor.getValue().getUrl()).isEqualTo(new URL("http://localhost:8181/test"));
URL expectedUrl = new URL("http://localhost:8181/test");
then(connection).should()
.getResponse(assertArg((request) -> assertThat(request.getUrl()).isEqualTo(expectedUrl)));
}
private WebConnection mockConnection() throws IOException {