Replace Mockito argument captors with assertArg
See gh-35015
This commit is contained in:
committed by
Moritz Halbritter
parent
4d14d0e437
commit
b61834c92d
@@ -23,19 +23,17 @@ import java.util.Set;
|
||||
|
||||
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.boot.devtools.filewatch.ChangedFile;
|
||||
import org.springframework.boot.devtools.filewatch.ChangedFiles;
|
||||
import org.springframework.boot.devtools.filewatch.FileSystemWatcher;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.mockito.ArgumentMatchers.assertArg;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
import static org.mockito.Mockito.never;
|
||||
@@ -57,9 +55,6 @@ class ClassPathFileChangeListenerTests {
|
||||
@Mock
|
||||
private FileSystemWatcher fileSystemWatcher;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<ApplicationEvent> eventCaptor;
|
||||
|
||||
@Test
|
||||
void eventPublisherMustNotBeNull() {
|
||||
assertThatIllegalArgumentException()
|
||||
@@ -102,10 +97,12 @@ class ClassPathFileChangeListenerTests {
|
||||
given(this.restartStrategy.isRestartRequired(file2)).willReturn(true);
|
||||
}
|
||||
listener.onChange(changeSet);
|
||||
then(this.eventPublisher).should().publishEvent(this.eventCaptor.capture());
|
||||
ClassPathChangedEvent actualEvent = (ClassPathChangedEvent) this.eventCaptor.getValue();
|
||||
assertThat(actualEvent.getChangeSet()).isEqualTo(changeSet);
|
||||
assertThat(actualEvent.isRestartRequired()).isEqualTo(restart);
|
||||
then(this.eventPublisher).should()
|
||||
.publishEvent(assertArg((applicationEvent) -> assertThat(applicationEvent)
|
||||
.isInstanceOfSatisfying(ClassPathChangedEvent.class, (classPathChangedEvent) -> {
|
||||
assertThat(classPathChangedEvent.getChangeSet()).isEqualTo(changeSet);
|
||||
assertThat(classPathChangedEvent.isRestartRequired()).isEqualTo(restart);
|
||||
})));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,8 +24,6 @@ import jakarta.servlet.http.HttpServletResponse;
|
||||
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 org.springframework.mock.web.MockHttpServletResponse;
|
||||
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.then;
|
||||
import static org.mockito.BDDMockito.willReturn;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -57,12 +56,6 @@ class DispatcherFilterTests {
|
||||
@Mock
|
||||
private FilterChain chain;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<ServerHttpResponse> serverResponseCaptor;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<ServerHttpRequest> serverRequestCaptor;
|
||||
|
||||
private DispatcherFilter filter;
|
||||
|
||||
@BeforeEach
|
||||
@@ -100,13 +93,15 @@ class DispatcherFilterTests {
|
||||
willReturn(true).given(this.dispatcher).handle(any(ServerHttpRequest.class), any(ServerHttpResponse.class));
|
||||
this.filter.doFilter(request, response, this.chain);
|
||||
then(this.chain).shouldHaveNoInteractions();
|
||||
then(this.dispatcher).should().handle(this.serverRequestCaptor.capture(), this.serverResponseCaptor.capture());
|
||||
ServerHttpRequest dispatcherRequest = this.serverRequestCaptor.getValue();
|
||||
ServletServerHttpRequest actualRequest = (ServletServerHttpRequest) dispatcherRequest;
|
||||
ServerHttpResponse dispatcherResponse = this.serverResponseCaptor.getValue();
|
||||
ServletServerHttpResponse actualResponse = (ServletServerHttpResponse) dispatcherResponse;
|
||||
assertThat(actualRequest.getServletRequest()).isEqualTo(request);
|
||||
assertThat(actualResponse.getServletResponse()).isEqualTo(response);
|
||||
then(this.dispatcher).should()
|
||||
.handle(assertArg((serverHttpRequest) -> assertThat(serverHttpRequest).isInstanceOfSatisfying(
|
||||
ServletServerHttpRequest.class,
|
||||
(servletServerHttpRequest) -> assertThat(servletServerHttpRequest.getServletRequest())
|
||||
.isEqualTo(request))),
|
||||
assertArg((serverHttpResponse) -> assertThat(serverHttpResponse).isInstanceOfSatisfying(
|
||||
ServletServerHttpResponse.class,
|
||||
(servletServerHttpResponse) -> assertThat(servletServerHttpResponse.getServletResponse())
|
||||
.isEqualTo(response))));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -23,8 +23,6 @@ import java.io.ObjectOutputStream;
|
||||
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;
|
||||
|
||||
@@ -38,6 +36,7 @@ import org.springframework.mock.web.MockHttpServletResponse;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.mockito.ArgumentMatchers.assertArg;
|
||||
import static org.mockito.BDDMockito.then;
|
||||
|
||||
/**
|
||||
@@ -53,9 +52,6 @@ class HttpRestartServerTests {
|
||||
|
||||
private HttpRestartServer server;
|
||||
|
||||
@Captor
|
||||
private ArgumentCaptor<ClassLoaderFiles> filesCaptor;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
this.server = new HttpRestartServer(this.delegate);
|
||||
@@ -82,8 +78,9 @@ class HttpRestartServerTests {
|
||||
byte[] bytes = serialize(files);
|
||||
request.setContent(bytes);
|
||||
this.server.handle(new ServletServerHttpRequest(request), new ServletServerHttpResponse(response));
|
||||
then(this.delegate).should().updateAndRestart(this.filesCaptor.capture());
|
||||
assertThat(this.filesCaptor.getValue().getFile("name")).isNotNull();
|
||||
then(this.delegate).should()
|
||||
.updateAndRestart(
|
||||
assertArg((classLoaderFiles) -> assertThat(classLoaderFiles.getFile("name")).isNotNull()));
|
||||
assertThat(response.getStatus()).isEqualTo(200);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user