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

@@ -24,11 +24,12 @@ import java.net.URI;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.boot.buildpack.platform.docker.DockerApi.ContainerApi;
import org.springframework.boot.buildpack.platform.docker.DockerApi.ImageApi;
@@ -64,6 +65,7 @@ import static org.mockito.Mockito.verify;
* @author Phillip Webb
* @author Scott Frederick
*/
@ExtendWith(MockitoExtension.class)
class DockerApiTests {
private static final String API_URL = "/" + DockerApi.API_VERSION;
@@ -81,7 +83,6 @@ class DockerApiTests {
@BeforeEach
void setup() {
MockitoAnnotations.initMocks(this);
this.dockerApi = new DockerApi(this.http);
}
@@ -127,7 +128,6 @@ class DockerApiTests {
@BeforeEach
void setup() {
MockitoAnnotations.initMocks(this);
this.api = DockerApiTests.this.dockerApi.image();
}
@@ -232,7 +232,6 @@ class DockerApiTests {
@BeforeEach
void setup() {
MockitoAnnotations.initMocks(this);
this.api = DockerApiTests.this.dockerApi.container();
}
@@ -370,7 +369,6 @@ class DockerApiTests {
@BeforeEach
void setup() {
MockitoAnnotations.initMocks(this);
this.api = DockerApiTests.this.dockerApi.volume();
}

View File

@@ -37,10 +37,11 @@ import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
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.buildpack.platform.docker.transport.HttpTransport.Response;
import org.springframework.util.StreamUtils;
@@ -58,6 +59,7 @@ import static org.mockito.Mockito.verify;
* @author Mike Smithson
* @author Scott Frederick
*/
@ExtendWith(MockitoExtension.class)
class HttpClientTransportTests {
private static final String APPLICATION_JSON = "application/json";
@@ -89,16 +91,13 @@ class HttpClientTransportTests {
@BeforeEach
void setup() throws Exception {
MockitoAnnotations.initMocks(this);
given(this.client.execute(any(HttpHost.class), any(HttpRequest.class))).willReturn(this.response);
given(this.response.getEntity()).willReturn(this.entity);
given(this.response.getStatusLine()).willReturn(this.statusLine);
this.http = new TestHttpClientTransport(this.client);
this.uri = new URI("example");
}
@Test
void getShouldExecuteHttpGet() throws Exception {
givenClientWillReturnResponse();
given(this.entity.getContent()).willReturn(this.content);
given(this.statusLine.getStatusCode()).willReturn(200);
Response response = this.http.get(this.uri);
@@ -112,6 +111,7 @@ class HttpClientTransportTests {
@Test
void postShouldExecuteHttpPost() throws Exception {
givenClientWillReturnResponse();
given(this.entity.getContent()).willReturn(this.content);
given(this.statusLine.getStatusCode()).willReturn(200);
Response response = this.http.post(this.uri);
@@ -125,6 +125,7 @@ class HttpClientTransportTests {
@Test
void postWithContentShouldExecuteHttpPost() throws Exception {
givenClientWillReturnResponse();
given(this.entity.getContent()).willReturn(this.content);
given(this.statusLine.getStatusCode()).willReturn(200);
Response response = this.http.post(this.uri, APPLICATION_JSON,
@@ -145,6 +146,7 @@ class HttpClientTransportTests {
@Test
void putWithContentShouldExecuteHttpPut() throws Exception {
givenClientWillReturnResponse();
given(this.entity.getContent()).willReturn(this.content);
given(this.statusLine.getStatusCode()).willReturn(200);
Response response = this.http.put(this.uri, APPLICATION_JSON,
@@ -165,6 +167,7 @@ class HttpClientTransportTests {
@Test
void deleteShouldExecuteHttpDelete() throws IOException {
givenClientWillReturnResponse();
given(this.entity.getContent()).willReturn(this.content);
given(this.statusLine.getStatusCode()).willReturn(200);
Response response = this.http.delete(this.uri);
@@ -178,6 +181,7 @@ class HttpClientTransportTests {
@Test
void executeWhenResponseIsIn400RangeShouldThrowDockerException() throws IOException {
givenClientWillReturnResponse();
given(this.entity.getContent()).willReturn(getClass().getResourceAsStream("errors.json"));
given(this.statusLine.getStatusCode()).willReturn(404);
assertThatExceptionOfType(DockerEngineException.class).isThrownBy(() -> this.http.get(this.uri))
@@ -188,7 +192,8 @@ class HttpClientTransportTests {
}
@Test
void executeWhenResponseIsIn500RangeWithNoContentShouldThrowDockerException() {
void executeWhenResponseIsIn500RangeWithNoContentShouldThrowDockerException() throws IOException {
givenClientWillReturnResponse();
given(this.statusLine.getStatusCode()).willReturn(500);
assertThatExceptionOfType(DockerEngineException.class).isThrownBy(() -> this.http.get(this.uri))
.satisfies((ex) -> {
@@ -199,6 +204,7 @@ class HttpClientTransportTests {
@Test
void executeWhenResponseIsIn500RangeWithMessageShouldThrowDockerException() throws IOException {
givenClientWillReturnResponse();
given(this.entity.getContent()).willReturn(getClass().getResourceAsStream("message.json"));
given(this.statusLine.getStatusCode()).willReturn(500);
assertThatExceptionOfType(DockerEngineException.class).isThrownBy(() -> this.http.get(this.uri))
@@ -210,6 +216,7 @@ class HttpClientTransportTests {
@Test
void executeWhenResponseIsIn500RangeWithOtherContentShouldThrowDockerException() throws IOException {
givenClientWillReturnResponse();
given(this.entity.getContent()).willReturn(this.content);
given(this.statusLine.getStatusCode()).willReturn(500);
assertThatExceptionOfType(DockerEngineException.class).isThrownBy(() -> this.http.get(this.uri))
@@ -233,6 +240,12 @@ class HttpClientTransportTests {
return new String(out.toByteArray(), StandardCharsets.UTF_8);
}
private void givenClientWillReturnResponse() throws IOException {
given(this.client.execute(any(HttpHost.class), any(HttpRequest.class))).willReturn(this.response);
given(this.response.getEntity()).willReturn(this.entity);
given(this.response.getStatusLine()).willReturn(this.statusLine);
}
/**
* Test {@link HttpClientTransport} implementation.
*/

View File

@@ -13,4 +13,5 @@ dependencies {
testImplementation("org.assertj:assertj-core")
testImplementation("org.junit.jupiter:junit-jupiter")
testImplementation("org.mockito:mockito-core")
testImplementation("org.mockito:mockito-junit-jupiter")
}

View File

@@ -27,9 +27,10 @@ import java.util.zip.ZipOutputStream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
@@ -39,6 +40,7 @@ import static org.mockito.BDDMockito.given;
*
* @author Phillip Webb
*/
@ExtendWith(MockitoExtension.class)
class ExtractCommandTests {
@TempDir
@@ -57,17 +59,16 @@ class ExtractCommandTests {
@BeforeEach
void setup() throws Exception {
MockitoAnnotations.initMocks(this);
this.jarFile = createJarFile("test.jar");
this.extract = new File(this.temp, "extract");
this.extract.mkdir();
given(this.context.getJarFile()).willReturn(this.jarFile);
given(this.context.getWorkingDir()).willReturn(this.extract);
this.command = new ExtractCommand(this.context, this.layers);
}
@Test
void runExtractsLayers() throws Exception {
given(this.context.getJarFile()).willReturn(this.jarFile);
given(this.context.getWorkingDir()).willReturn(this.extract);
this.command.run(Collections.emptyMap(), Collections.emptyList());
assertThat(this.extract.list()).containsOnly("a", "b", "c", "d");
assertThat(new File(this.extract, "a/a/a.jar")).exists();
@@ -78,6 +79,7 @@ class ExtractCommandTests {
@Test
void runWhenHasDestinationOptionExtractsLayers() {
given(this.context.getJarFile()).willReturn(this.jarFile);
File out = new File(this.extract, "out");
this.command.run(Collections.singletonMap(ExtractCommand.DESTINATION_OPTION, out.getAbsolutePath()),
Collections.emptyList());
@@ -89,6 +91,8 @@ class ExtractCommandTests {
@Test
void runWhenHasLayerParamsExtractsLimitedLayers() {
given(this.context.getJarFile()).willReturn(this.jarFile);
given(this.context.getWorkingDir()).willReturn(this.extract);
this.command.run(Collections.emptyMap(), Arrays.asList("a", "c"));
assertThat(this.extract.list()).containsOnly("a", "c");
assertThat(new File(this.extract, "a/a/a.jar")).exists();

View File

@@ -28,9 +28,10 @@ import java.util.zip.ZipOutputStream;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
@@ -41,6 +42,7 @@ import static org.mockito.BDDMockito.given;
* @author Phillip Webb
* @author Madhura Bhave
*/
@ExtendWith(MockitoExtension.class)
class ListCommandTests {
@TempDir
@@ -51,19 +53,18 @@ class ListCommandTests {
private File jarFile;
private ListCommand command;
private TestPrintStream out;
@BeforeEach
void setup() throws Exception {
MockitoAnnotations.initMocks(this);
this.jarFile = createJarFile("test.jar");
given(this.context.getJarFile()).willReturn(this.jarFile);
this.command = new ListCommand(this.context);
this.out = new TestPrintStream(this);
}
private ListCommand command;
private TestPrintStream out;
@Test
void listLayersShouldListLayers() {
Layers layers = IndexedLayers.get(this.context);

View File

@@ -36,6 +36,7 @@ dependencies {
testImplementation("org.assertj:assertj-core")
testImplementation("org.junit.jupiter:junit-jupiter")
testImplementation("org.mockito:mockito-core")
testImplementation("org.mockito:mockito-junit-jupiter")
testImplementation("org.springframework:spring-core")
}

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.
@@ -28,10 +28,11 @@ import org.apache.maven.model.Dependency;
import org.apache.maven.plugin.logging.Log;
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.loader.tools.Library;
import org.springframework.boot.loader.tools.LibraryCallback;
@@ -48,6 +49,7 @@ import static org.mockito.Mockito.verify;
*
* @author Phillip Webb
*/
@ExtendWith(MockitoExtension.class)
class ArtifactsLibrariesTests {
@Mock
@@ -70,17 +72,15 @@ class ArtifactsLibrariesTests {
@BeforeEach
void setup() {
MockitoAnnotations.initMocks(this);
this.artifacts = Collections.singleton(this.artifact);
this.libs = new ArtifactsLibraries(this.artifacts, null, mock(Log.class));
given(this.artifact.getFile()).willReturn(this.file);
given(this.artifactHandler.getExtension()).willReturn("jar");
given(this.artifact.getArtifactHandler()).willReturn(this.artifactHandler);
}
@Test
void callbackForJars() throws Exception {
given(this.artifact.getType()).willReturn("jar");
given(this.artifact.getFile()).willReturn(this.file);
given(this.artifact.getArtifactHandler()).willReturn(this.artifactHandler);
given(this.artifact.getScope()).willReturn("compile");
this.libs.doWithLibraries(this.callback);
verify(this.callback).library(this.libraryCaptor.capture());
@@ -92,9 +92,10 @@ class ArtifactsLibrariesTests {
@Test
void callbackWithUnpack() throws Exception {
given(this.artifact.getFile()).willReturn(this.file);
given(this.artifact.getArtifactHandler()).willReturn(this.artifactHandler);
given(this.artifact.getGroupId()).willReturn("gid");
given(this.artifact.getArtifactId()).willReturn("aid");
given(this.artifact.getType()).willReturn("jar");
given(this.artifact.getScope()).willReturn("compile");
Dependency unpack = new Dependency();
unpack.setGroupId("gid");
@@ -109,14 +110,12 @@ class ArtifactsLibrariesTests {
void renamesDuplicates() throws Exception {
Artifact artifact1 = mock(Artifact.class);
Artifact artifact2 = mock(Artifact.class);
given(artifact1.getType()).willReturn("jar");
given(artifact1.getScope()).willReturn("compile");
given(artifact1.getGroupId()).willReturn("g1");
given(artifact1.getArtifactId()).willReturn("artifact");
given(artifact1.getBaseVersion()).willReturn("1.0");
given(artifact1.getFile()).willReturn(new File("a"));
given(artifact1.getArtifactHandler()).willReturn(this.artifactHandler);
given(artifact2.getType()).willReturn("jar");
given(artifact2.getScope()).willReturn("compile");
given(artifact2.getGroupId()).willReturn("g2");
given(artifact2.getArtifactId()).willReturn("artifact");

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.
@@ -25,7 +25,6 @@ import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterRegistration;
import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
@@ -34,6 +33,7 @@ import javax.servlet.ServletRegistration;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
/**
@@ -66,27 +66,26 @@ public abstract class MockServletWebServer {
private void initialize() {
try {
this.servletContext = mock(ServletContext.class);
given(this.servletContext.addServlet(anyString(), any(Servlet.class))).willAnswer((invocation) -> {
lenient().doAnswer((invocation) -> {
RegisteredServlet registeredServlet = new RegisteredServlet(invocation.getArgument(1));
MockServletWebServer.this.registeredServlets.add(registeredServlet);
return registeredServlet.getRegistration();
});
given(this.servletContext.addFilter(anyString(), any(Filter.class))).willAnswer((invocation) -> {
}).when(this.servletContext).addServlet(anyString(), any(Servlet.class));
lenient().doAnswer((invocation) -> {
RegisteredFilter registeredFilter = new RegisteredFilter(invocation.getArgument(1));
MockServletWebServer.this.registeredFilters.add(registeredFilter);
return registeredFilter.getRegistration();
});
}).when(this.servletContext).addFilter(anyString(), any(Filter.class));
final Map<String, String> initParameters = new HashMap<>();
given(this.servletContext.setInitParameter(anyString(), anyString())).will((invocation) -> {
lenient().doAnswer((invocation) -> {
initParameters.put(invocation.getArgument(0), invocation.getArgument(1));
return null;
});
}).when(this.servletContext).setInitParameter(anyString(), anyString());
given(this.servletContext.getInitParameterNames())
.willReturn(Collections.enumeration(initParameters.keySet()));
given(this.servletContext.getInitParameter(anyString()))
.willAnswer((invocation) -> initParameters.get(invocation.getArgument(0)));
lenient().doAnswer((invocation) -> initParameters.get(invocation.getArgument(0))).when(this.servletContext)
.getInitParameter(anyString());
given(this.servletContext.getAttributeNames()).willReturn(Collections.emptyEnumeration());
given(this.servletContext.getNamedDispatcher("default")).willReturn(mock(RequestDispatcher.class));
for (Initializer initializer : this.initializers) {
initializer.onStartup(this.servletContext);
}