Upgrade to Mockito 3.4.6

Closes gh-22838
This commit is contained in:
Andy Wilkinson
2020-08-08 15:53:42 +01:00
parent f2a52a87ec
commit 969dd35e45
79 changed files with 444 additions and 443 deletions

View File

@@ -21,12 +21,12 @@ import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
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.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.boot.devtools.filewatch.ChangedFile;
import org.springframework.boot.devtools.filewatch.ChangedFiles;
@@ -45,6 +45,7 @@ import static org.mockito.Mockito.verify;
*
* @author Phillip Webb
*/
@ExtendWith(MockitoExtension.class)
class ClassPathFileChangeListenerTests {
@Mock
@@ -59,11 +60,6 @@ class ClassPathFileChangeListenerTests {
@Captor
private ArgumentCaptor<ApplicationEvent> eventCaptor;
@BeforeEach
void setup() {
MockitoAnnotations.initMocks(this);
}
@Test
void eventPublisherMustNotBeNull() {
assertThatIllegalArgumentException()

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,8 +21,9 @@ 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.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.boot.devtools.autoconfigure.OptionalLiveReloadServer;
import org.springframework.http.HttpMethod;
@@ -42,6 +43,7 @@ import static org.mockito.Mockito.verify;
*
* @author Phillip Webb
*/
@ExtendWith(MockitoExtension.class)
class DelayedLiveReloadTriggerTests {
private static final String URL = "http://localhost:8080";
@@ -68,11 +70,6 @@ class DelayedLiveReloadTriggerTests {
@BeforeEach
void setup() throws IOException {
MockitoAnnotations.initMocks(this);
given(this.errorRequest.execute()).willReturn(this.errorResponse);
given(this.okRequest.execute()).willReturn(this.okResponse);
given(this.errorResponse.getStatusCode()).willReturn(HttpStatus.INTERNAL_SERVER_ERROR);
given(this.okResponse.getStatusCode()).willReturn(HttpStatus.OK);
this.trigger = new DelayedLiveReloadTrigger(this.liveReloadServer, this.requestFactory, URL);
}
@@ -106,6 +103,10 @@ class DelayedLiveReloadTriggerTests {
@Test
void triggerReloadOnStatus() throws Exception {
given(this.errorRequest.execute()).willReturn(this.errorResponse);
given(this.okRequest.execute()).willReturn(this.okResponse);
given(this.errorResponse.getStatusCode()).willReturn(HttpStatus.INTERNAL_SERVER_ERROR);
given(this.okResponse.getStatusCode()).willReturn(HttpStatus.OK);
given(this.requestFactory.createRequest(new URI(URL), HttpMethod.GET)).willThrow(new IOException())
.willReturn(this.errorRequest, this.okRequest);
long startTime = System.currentTimeMillis();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,8 +20,9 @@ import java.io.IOException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
@@ -39,6 +40,7 @@ import static org.mockito.BDDMockito.given;
* @author Rob Winch
* @since 1.3.0
*/
@ExtendWith(MockitoExtension.class)
class HttpHeaderInterceptorTests {
private String name;
@@ -61,13 +63,11 @@ class HttpHeaderInterceptorTests {
@BeforeEach
void setup() throws Exception {
MockitoAnnotations.initMocks(this);
this.body = new byte[] {};
this.httpRequest = new MockHttpServletRequest();
this.request = new ServletServerHttpRequest(this.httpRequest);
this.name = "X-AUTH-TOKEN";
this.value = "secret";
given(this.execution.execute(this.request, this.body)).willReturn(this.response);
this.interceptor = new HttpHeaderInterceptor(this.name, this.value);
}
@@ -97,6 +97,7 @@ class HttpHeaderInterceptorTests {
@Test
void intercept() throws IOException {
given(this.execution.execute(this.request, this.body)).willReturn(this.response);
ClientHttpResponse result = this.interceptor.intercept(this.request, this.body, this.execution);
assertThat(this.request.getHeaders().getFirst(this.name)).isEqualTo(this.value);
assertThat(result).isEqualTo(this.response);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,10 +24,11 @@ import javax.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.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
@@ -49,6 +50,7 @@ import static org.mockito.Mockito.verifyNoInteractions;
*
* @author Phillip Webb
*/
@ExtendWith(MockitoExtension.class)
class DispatcherFilterTests {
@Mock
@@ -67,7 +69,6 @@ class DispatcherFilterTests {
@BeforeEach
void setup() {
MockitoAnnotations.initMocks(this);
this.filter = new DispatcherFilter(this.dispatcher);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,11 +20,11 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.core.Ordered;
import org.springframework.http.server.ServerHttpRequest;
@@ -49,27 +49,17 @@ import static org.mockito.Mockito.withSettings;
*
* @author Phillip Webb
*/
@ExtendWith(MockitoExtension.class)
class DispatcherTests {
@Mock
private AccessManager accessManager;
private MockHttpServletRequest request;
private MockHttpServletResponse response = new MockHttpServletResponse();
private MockHttpServletResponse response;
private ServerHttpRequest serverRequest = new ServletServerHttpRequest(new MockHttpServletRequest());
private ServerHttpRequest serverRequest;
private ServerHttpResponse serverResponse;
@BeforeEach
void setup() {
MockitoAnnotations.initMocks(this);
this.request = new MockHttpServletRequest();
this.response = new MockHttpServletResponse();
this.serverRequest = new ServletServerHttpRequest(this.request);
this.serverResponse = new ServletServerHttpResponse(this.response);
}
private ServerHttpResponse serverResponse = new ServletServerHttpResponse(this.response);
@Test
void accessManagerMustNotBeNull() {

View File

@@ -22,10 +22,11 @@ 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.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.boot.devtools.restart.classloader.ClassLoaderFile;
import org.springframework.boot.devtools.restart.classloader.ClassLoaderFile.Kind;
@@ -45,6 +46,7 @@ import static org.mockito.Mockito.verifyNoInteractions;
*
* @author Phillip Webb
*/
@ExtendWith(MockitoExtension.class)
class HttpRestartServerTests {
@Mock
@@ -57,7 +59,6 @@ class HttpRestartServerTests {
@BeforeEach
void setup() {
MockitoAnnotations.initMocks(this);
this.server = new HttpRestartServer(this.delegate);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,7 +29,7 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.boot.devtools.test.MockClientHttpRequestFactory;
import org.springframework.boot.devtools.tunnel.client.HttpTunnelConnection.TunnelChannel;
@@ -50,7 +50,7 @@ import static org.mockito.Mockito.verify;
* @author Rob Winch
* @author Andy Wilkinson
*/
@ExtendWith(OutputCaptureExtension.class)
@ExtendWith({ OutputCaptureExtension.class, MockitoExtension.class })
class HttpTunnelConnectionTests {
private String url;
@@ -66,7 +66,6 @@ class HttpTunnelConnectionTests {
@BeforeEach
void setup() {
MockitoAnnotations.initMocks(this);
this.url = "http://localhost:12345";
this.incomingData = new ByteArrayOutputStream();
this.incomingChannel = Channels.newChannel(this.incomingData);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,8 +29,9 @@ import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.boot.devtools.tunnel.payload.HttpTunnelPayload;
import org.springframework.boot.devtools.tunnel.server.HttpTunnelServer.HttpConnection;
@@ -57,6 +58,7 @@ import static org.mockito.Mockito.verify;
*
* @author Phillip Webb
*/
@ExtendWith(MockitoExtension.class)
class HttpTunnelServerTests {
private static final int DEFAULT_LONG_POLL_TIMEOUT = 10000;
@@ -82,13 +84,7 @@ class HttpTunnelServerTests {
@BeforeEach
void setup() throws Exception {
MockitoAnnotations.initMocks(this);
this.server = new HttpTunnelServer(this.serverConnection);
given(this.serverConnection.open(anyInt())).willAnswer((invocation) -> {
MockServerChannel channel = HttpTunnelServerTests.this.serverChannel;
channel.setTimeout(invocation.getArgument(0));
return channel;
});
this.servletRequest = new MockHttpServletRequest();
this.servletRequest.setAsyncSupported(true);
this.servletResponse = new MockHttpServletResponse();
@@ -106,12 +102,14 @@ class HttpTunnelServerTests {
@Test
void serverConnectedOnFirstRequest() throws Exception {
verify(this.serverConnection, never()).open(anyInt());
givenServerConnectionOpenWillAnswerWithServerChannel();
this.server.handle(this.request, this.response);
verify(this.serverConnection, times(1)).open(DEFAULT_LONG_POLL_TIMEOUT);
}
@Test
void longPollTimeout() throws Exception {
givenServerConnectionOpenWillAnswerWithServerChannel();
this.server.setLongPollTimeout(800);
this.server.handle(this.request, this.response);
verify(this.serverConnection, times(1)).open(800);
@@ -125,6 +123,7 @@ class HttpTunnelServerTests {
@Test
void initialRequestIsSentToServer() throws Exception {
givenServerConnectionOpenWillAnswerWithServerChannel();
this.servletRequest.addHeader(SEQ_HEADER, "1");
this.servletRequest.setContent("hello".getBytes());
this.server.handle(this.request, this.response);
@@ -135,6 +134,7 @@ class HttpTunnelServerTests {
@Test
void initialRequestIsUsedForFirstServerResponse() throws Exception {
givenServerConnectionOpenWillAnswerWithServerChannel();
this.servletRequest.addHeader(SEQ_HEADER, "1");
this.servletRequest.setContent("hello".getBytes());
this.server.handle(this.request, this.response);
@@ -148,6 +148,7 @@ class HttpTunnelServerTests {
@Test
void initialRequestHasNoPayload() throws Exception {
givenServerConnectionOpenWillAnswerWithServerChannel();
this.server.handle(this.request, this.response);
this.serverChannel.disconnect();
this.server.getServerThread().join();
@@ -156,6 +157,7 @@ class HttpTunnelServerTests {
@Test
void typicalRequestResponseTraffic() throws Exception {
givenServerConnectionOpenWillAnswerWithServerChannel();
MockHttpConnection h1 = new MockHttpConnection();
this.server.handle(h1);
MockHttpConnection h2 = new MockHttpConnection("hello server", 1);
@@ -177,6 +179,7 @@ class HttpTunnelServerTests {
@Test
void clientIsAwareOfServerClose() throws Exception {
givenServerConnectionOpenWillAnswerWithServerChannel();
MockHttpConnection h1 = new MockHttpConnection("1", 1);
this.server.handle(h1);
this.serverChannel.disconnect();
@@ -186,6 +189,7 @@ class HttpTunnelServerTests {
@Test
void clientCanCloseServer() throws Exception {
givenServerConnectionOpenWillAnswerWithServerChannel();
MockHttpConnection h1 = new MockHttpConnection();
this.server.handle(h1);
MockHttpConnection h2 = new MockHttpConnection("DISCONNECT", 1);
@@ -198,6 +202,7 @@ class HttpTunnelServerTests {
@Test
void neverMoreThanTwoHttpConnections() throws Exception {
givenServerConnectionOpenWillAnswerWithServerChannel();
MockHttpConnection h1 = new MockHttpConnection();
this.server.handle(h1);
MockHttpConnection h2 = new MockHttpConnection("1", 2);
@@ -212,6 +217,7 @@ class HttpTunnelServerTests {
@Test
void requestReceivedOutOfOrder() throws Exception {
givenServerConnectionOpenWillAnswerWithServerChannel();
MockHttpConnection h1 = new MockHttpConnection();
MockHttpConnection h2 = new MockHttpConnection("1+2", 1);
MockHttpConnection h3 = new MockHttpConnection("+3", 2);
@@ -225,6 +231,7 @@ class HttpTunnelServerTests {
@Test
void httpConnectionsAreClosedAfterLongPollTimeout() throws Exception {
givenServerConnectionOpenWillAnswerWithServerChannel();
this.server.setDisconnectTimeout(1000);
this.server.setLongPollTimeout(100);
MockHttpConnection h1 = new MockHttpConnection();
@@ -240,6 +247,7 @@ class HttpTunnelServerTests {
@Test
void disconnectTimeout() throws Exception {
givenServerConnectionOpenWillAnswerWithServerChannel();
this.server.setDisconnectTimeout(100);
this.server.setLongPollTimeout(100);
MockHttpConnection h1 = new MockHttpConnection();
@@ -317,6 +325,14 @@ class HttpTunnelServerTests {
assertThat(connection.isOlderThan(100)).isTrue();
}
private void givenServerConnectionOpenWillAnswerWithServerChannel() throws IOException {
given(this.serverConnection.open(anyInt())).willAnswer((invocation) -> {
MockServerChannel channel = HttpTunnelServerTests.this.serverChannel;
channel.setTimeout(invocation.getArgument(0));
return channel;
});
}
/**
* Mock {@link ByteChannel} used to simulate the server connection.
*/