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

@@ -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,10 +20,10 @@ import java.util.Collections;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
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.amqp.rabbit.core.ChannelCallback;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
@@ -41,6 +41,7 @@ import static org.mockito.Mockito.mock;
*
* @author Phillip Webb
*/
@ExtendWith(MockitoExtension.class)
class RabbitHealthIndicatorTests {
@Mock
@@ -49,15 +50,6 @@ class RabbitHealthIndicatorTests {
@Mock
private Channel channel;
@BeforeEach
void setup() {
MockitoAnnotations.initMocks(this);
given(this.rabbitTemplate.execute(any())).willAnswer((invocation) -> {
ChannelCallback<?> callback = invocation.getArgument(0);
return callback.doInRabbit(this.channel);
});
}
@Test
void createWhenRabbitTemplateIsNullShouldThrowException() {
assertThatIllegalArgumentException().isThrownBy(() -> new RabbitHealthIndicator(null))
@@ -66,6 +58,7 @@ class RabbitHealthIndicatorTests {
@Test
void healthWhenConnectionSucceedsShouldReturnUpWithVersion() {
givenTemplateExecutionWillInvokeCallback();
Connection connection = mock(Connection.class);
given(this.channel.getConnection()).willReturn(connection);
given(connection.getServerProperties()).willReturn(Collections.singletonMap("version", "123"));
@@ -76,9 +69,17 @@ class RabbitHealthIndicatorTests {
@Test
void healthWhenConnectionFailsShouldReturnDown() {
givenTemplateExecutionWillInvokeCallback();
given(this.channel.getConnection()).willThrow(new RuntimeException());
Health health = new RabbitHealthIndicator(this.rabbitTemplate).health();
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
}
private void givenTemplateExecutionWillInvokeCallback() {
given(this.rabbitTemplate.execute(any())).willAnswer((invocation) -> {
ChannelCallback<?> callback = invocation.getArgument(0);
return callback.doInRabbit(this.channel);
});
}
}

View File

@@ -16,10 +16,10 @@
package org.springframework.boot.actuate.availability;
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.actuate.health.Status;
import org.springframework.boot.availability.ApplicationAvailability;
@@ -35,16 +35,12 @@ import static org.mockito.BDDMockito.given;
*
* @author Phillip Webb
*/
@ExtendWith(MockitoExtension.class)
class AvailabilityStateHealthIndicatorTests {
@Mock
private ApplicationAvailability applicationAvailability;
@BeforeEach
void setup() {
MockitoAnnotations.initMocks(this);
}
@Test
void createWhenApplicationAvailabilityIsNullThrowsException() {
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.util.function.Function;
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.actuate.endpoint.EndpointId;
import org.springframework.boot.actuate.endpoint.OperationType;
@@ -45,6 +46,7 @@ import static org.mockito.Mockito.verify;
* @author Phillip Webb
* @author Stephane Nicoll
*/
@ExtendWith(MockitoExtension.class)
class CachingOperationInvokerAdvisorTests {
@Mock
@@ -57,7 +59,6 @@ class CachingOperationInvokerAdvisorTests {
@BeforeEach
void setup() {
MockitoAnnotations.initMocks(this);
this.advisor = new CachingOperationInvokerAdvisor(this.timeToLive);
}

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.
@@ -27,10 +27,12 @@ import javax.management.ObjectName;
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.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.jmx.JmxException;
import org.springframework.jmx.export.MBeanExportException;
@@ -42,7 +44,6 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.willThrow;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
/**
@@ -51,12 +52,14 @@ import static org.mockito.Mockito.verify;
* @author Stephane Nicoll
* @author Phillip Webb
*/
@ExtendWith(MockitoExtension.class)
class JmxEndpointExporterTests {
@Mock
private MBeanServer mBeanServer;
private EndpointObjectNameFactory objectNameFactory = spy(new TestEndpointObjectNameFactory());
@Spy
private EndpointObjectNameFactory objectNameFactory = new TestEndpointObjectNameFactory();
private JmxOperationResponseMapper responseMapper = new TestJmxOperationResponseMapper();
@@ -72,7 +75,6 @@ class JmxEndpointExporterTests {
@BeforeEach
void setup() {
MockitoAnnotations.initMocks(this);
this.exporter = new JmxEndpointExporter(this.mBeanServer, this.objectNameFactory, this.responseMapper,
this.endpoints);
}

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.
@@ -26,12 +26,12 @@ import javax.servlet.ServletRegistration.Dynamic;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
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.actuate.endpoint.EndpointId;
@@ -49,6 +49,7 @@ import static org.mockito.Mockito.verify;
* @author Phillip Webb
* @author Stephane Nicoll
*/
@ExtendWith(MockitoExtension.class)
class ServletEndpointRegistrarTests {
@Mock
@@ -60,12 +61,6 @@ class ServletEndpointRegistrarTests {
@Captor
private ArgumentCaptor<Servlet> servlet;
@BeforeEach
void setup() {
MockitoAnnotations.initMocks(this);
given(this.servletContext.addServlet(any(String.class), any(Servlet.class))).willReturn(this.dynamic);
}
@Test
void createWhenServletEndpointsIsNullShouldThrowException() {
assertThatIllegalArgumentException().isThrownBy(() -> new ServletEndpointRegistrar(null, null))
@@ -93,6 +88,7 @@ class ServletEndpointRegistrarTests {
}
private void assertBasePath(String basePath, String expectedMapping) throws ServletException {
given(this.servletContext.addServlet(any(String.class), any(Servlet.class))).willReturn(this.dynamic);
ExposableServletEndpoint endpoint = mockEndpoint(new EndpointServlet(TestServlet.class));
ServletEndpointRegistrar registrar = new ServletEndpointRegistrar(basePath, Collections.singleton(endpoint));
registrar.onStartup(this.servletContext);
@@ -103,6 +99,7 @@ class ServletEndpointRegistrarTests {
@Test
void onStartupWhenHasInitParametersShouldRegisterInitParameters() throws Exception {
given(this.servletContext.addServlet(any(String.class), any(Servlet.class))).willReturn(this.dynamic);
ExposableServletEndpoint endpoint = mockEndpoint(
new EndpointServlet(TestServlet.class).withInitParameter("a", "b"));
ServletEndpointRegistrar registrar = new ServletEndpointRegistrar("/actuator", Collections.singleton(endpoint));
@@ -112,6 +109,7 @@ class ServletEndpointRegistrarTests {
@Test
void onStartupWhenHasLoadOnStartupShouldRegisterLoadOnStartup() throws Exception {
given(this.servletContext.addServlet(any(String.class), any(Servlet.class))).willReturn(this.dynamic);
ExposableServletEndpoint endpoint = mockEndpoint(new EndpointServlet(TestServlet.class).withLoadOnStartup(7));
ServletEndpointRegistrar registrar = new ServletEndpointRegistrar("/actuator", Collections.singleton(endpoint));
registrar.onStartup(this.servletContext);
@@ -120,6 +118,7 @@ class ServletEndpointRegistrarTests {
@Test
void onStartupWhenHasNotLoadOnStartupShouldRegisterDefaultValue() throws Exception {
given(this.servletContext.addServlet(any(String.class), any(Servlet.class))).willReturn(this.dynamic);
ExposableServletEndpoint endpoint = mockEndpoint(new EndpointServlet(TestServlet.class));
ServletEndpointRegistrar registrar = new ServletEndpointRegistrar("/actuator", Collections.singleton(endpoint));
registrar.onStartup(this.servletContext);

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,12 +24,12 @@ import java.util.concurrent.ScheduledFuture;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.PushGateway;
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.actuate.metrics.export.prometheus.PrometheusPushGatewayManager.PushGatewayTaskScheduler;
import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusPushGatewayManager.ShutdownOperation;
@@ -51,6 +51,7 @@ import static org.mockito.Mockito.verifyNoInteractions;
*
* @author Phillip Webb
*/
@ExtendWith(MockitoExtension.class)
class PrometheusPushGatewayManagerTests {
@Mock
@@ -59,6 +60,7 @@ class PrometheusPushGatewayManagerTests {
@Mock
private CollectorRegistry registry;
@Mock
private TaskScheduler scheduler;
private Duration pushRate = Duration.ofSeconds(1);
@@ -71,12 +73,6 @@ class PrometheusPushGatewayManagerTests {
@Mock
private ScheduledFuture<Object> future;
@BeforeEach
void setup() {
MockitoAnnotations.initMocks(this);
this.scheduler = mockScheduler(TaskScheduler.class);
}
@Test
void createWhenPushGatewayIsNullThrowsException() {
assertThatIllegalArgumentException().isThrownBy(() -> new PrometheusPushGatewayManager(null, this.registry,
@@ -122,7 +118,8 @@ class PrometheusPushGatewayManagerTests {
@Test
void shutdownWhenOwnsSchedulerDoesShutdownScheduler() {
PushGatewayTaskScheduler ownedScheduler = mockScheduler(PushGatewayTaskScheduler.class);
PushGatewayTaskScheduler ownedScheduler = givenScheduleAtFixedRateWillReturnFuture(
mock(PushGatewayTaskScheduler.class));
PrometheusPushGatewayManager manager = new PrometheusPushGatewayManager(this.pushGateway, this.registry,
ownedScheduler, this.pushRate, "job", this.groupingKey, null);
manager.shutdown();
@@ -131,7 +128,8 @@ class PrometheusPushGatewayManagerTests {
@Test
void shutdownWhenDoesNotOwnSchedulerDoesNotShutdownScheduler() {
ThreadPoolTaskScheduler otherScheduler = mockScheduler(ThreadPoolTaskScheduler.class);
ThreadPoolTaskScheduler otherScheduler = givenScheduleAtFixedRateWillReturnFuture(
mock(ThreadPoolTaskScheduler.class));
PrometheusPushGatewayManager manager = new PrometheusPushGatewayManager(this.pushGateway, this.registry,
otherScheduler, this.pushRate, "job", this.groupingKey, null);
manager.shutdown();
@@ -140,6 +138,7 @@ class PrometheusPushGatewayManagerTests {
@Test
void shutdownWhenShutdownOperationIsPushPerformsPushOnShutdown() throws Exception {
givenScheduleAtFixedRateWithReturnFuture();
PrometheusPushGatewayManager manager = new PrometheusPushGatewayManager(this.pushGateway, this.registry,
this.scheduler, this.pushRate, "job", this.groupingKey, ShutdownOperation.PUSH);
manager.shutdown();
@@ -149,6 +148,7 @@ class PrometheusPushGatewayManagerTests {
@Test
void shutdownWhenShutdownOperationIsDeletePerformsDeleteOnShutdown() throws Exception {
givenScheduleAtFixedRateWithReturnFuture();
PrometheusPushGatewayManager manager = new PrometheusPushGatewayManager(this.pushGateway, this.registry,
this.scheduler, this.pushRate, "job", this.groupingKey, ShutdownOperation.DELETE);
manager.shutdown();
@@ -158,6 +158,7 @@ class PrometheusPushGatewayManagerTests {
@Test
void shutdownWhenShutdownOperationIsNoneDoesNothing() {
givenScheduleAtFixedRateWithReturnFuture();
PrometheusPushGatewayManager manager = new PrometheusPushGatewayManager(this.pushGateway, this.registry,
this.scheduler, this.pushRate, "job", this.groupingKey, ShutdownOperation.NONE);
manager.shutdown();
@@ -167,6 +168,7 @@ class PrometheusPushGatewayManagerTests {
@Test
void pushWhenUnknownHostExceptionIsThrownDoesShutdown() throws Exception {
givenScheduleAtFixedRateWithReturnFuture();
new PrometheusPushGatewayManager(this.pushGateway, this.registry, this.scheduler, this.pushRate, "job",
this.groupingKey, null);
verify(this.scheduler).scheduleAtFixedRate(this.task.capture(), eq(this.pushRate));
@@ -185,9 +187,12 @@ class PrometheusPushGatewayManagerTests {
this.task.getValue().run();
}
private void givenScheduleAtFixedRateWithReturnFuture() {
givenScheduleAtFixedRateWillReturnFuture(this.scheduler);
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private <T extends TaskScheduler> T mockScheduler(Class<T> type) {
T scheduler = mock(type);
private <T extends TaskScheduler> T givenScheduleAtFixedRateWillReturnFuture(T scheduler) {
given(scheduler.scheduleAtFixedRate(isA(Runnable.class), isA(Duration.class)))
.willReturn((ScheduledFuture) this.future);
return scheduler;

View File

@@ -20,8 +20,9 @@ import java.io.File;
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.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
@@ -37,6 +38,7 @@ import static org.mockito.BDDMockito.given;
* @author Mattias Severson
* @author Stephane Nicoll
*/
@ExtendWith(MockitoExtension.class)
class DiskSpaceHealthIndicatorTests {
private static final DataSize THRESHOLD = DataSize.ofKilobytes(1);
@@ -50,13 +52,12 @@ class DiskSpaceHealthIndicatorTests {
@BeforeEach
void setUp() {
MockitoAnnotations.initMocks(this);
given(this.fileMock.exists()).willReturn(true);
this.healthIndicator = new DiskSpaceHealthIndicator(this.fileMock, THRESHOLD);
}
@Test
void diskSpaceIsUp() {
given(this.fileMock.exists()).willReturn(true);
long freeSpace = THRESHOLD.toBytes() + 10;
given(this.fileMock.getUsableSpace()).willReturn(freeSpace);
given(this.fileMock.getTotalSpace()).willReturn(TOTAL_SPACE.toBytes());
@@ -70,6 +71,7 @@ class DiskSpaceHealthIndicatorTests {
@Test
void diskSpaceIsDown() {
given(this.fileMock.exists()).willReturn(true);
long freeSpace = THRESHOLD.toBytes() - 10;
given(this.fileMock.getUsableSpace()).willReturn(freeSpace);
given(this.fileMock.getTotalSpace()).willReturn(TOTAL_SPACE.toBytes());