Skip to content

Commit

Permalink
Upgrade Mockito to 4.0.0 (elastic#79949)
Browse files Browse the repository at this point in the history
Mockito 4.0 removes several deprecated methods. This commit updates
usages of those deprecated methods and upgrades mockito. The changes
include: * Replace anyMapOf,anyListOf,anySetOf,anyCollectionOf with the
same   method name without `Of` and no longer taking any arguments. *
Replace anyObject with any * Removing argument from isNull * Replace
verifyZeroInteractions with verifyNoMoreInteractions The changes here
were completely mechanical, done entirely with forms of find/replace
within IntelliJ.
  • Loading branch information
rjernst committed Oct 28, 2021
1 parent 9336cfd commit 99b6e3e
Show file tree
Hide file tree
Showing 86 changed files with 388 additions and 433 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

@Ignore("https://github.com/elastic/elasticsearch/issues/77190")
Expand Down Expand Up @@ -109,7 +109,7 @@ public void partitionFiles_withSnapshot_returnsSingleMapping() {
partitionedFiles,
hasEntry(equalTo(QualifiedVersion.of("8.0.0-SNAPSHOT")), hasItem(new File("docs/changelog/1234.yaml")))
);
verifyZeroInteractions(gitWrapper);
verifyNoMoreInteractions(gitWrapper);
}

/**
Expand All @@ -128,7 +128,7 @@ public void partitionFiles_withFirstRevision_returnsSingleMapping() {
// then:
assertThat(partitionedFiles, aMapWithSize(1));
assertThat(partitionedFiles, hasEntry(equalTo(QualifiedVersion.of("8.5.0")), hasItem(new File("docs/changelog/1234.yaml"))));
verifyZeroInteractions(gitWrapper);
verifyNoMoreInteractions(gitWrapper);
}

/**
Expand All @@ -146,7 +146,7 @@ public void partitionFiles_withFirstAlpha_returnsSingleMapping() {
// then:
assertThat(partitionedFiles, aMapWithSize(1));
assertThat(partitionedFiles, hasEntry(equalTo(QualifiedVersion.of("8.0.0-alpha1")), hasItem(new File("docs/changelog/1234.yaml"))));
verifyZeroInteractions(gitWrapper);
verifyNoMoreInteractions(gitWrapper);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyCollectionOf;
import static org.mockito.ArgumentMatchers.anyCollection;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -204,7 +204,7 @@ public void shutdown() {
int totalRuns = nodesSniffer.runs.get();
assertEquals(iters, totalRuns);
int setNodesRuns = totalRuns - nodesSniffer.failures.get() - nodesSniffer.emptyList.get();
verify(restClient, times(setNodesRuns)).setNodes(anyCollectionOf(Node.class));
verify(restClient, times(setNodesRuns)).setNodes(anyCollection());
verifyNoMoreInteractions(restClient);
}

Expand Down Expand Up @@ -270,7 +270,7 @@ public void shutdown() {}
int totalRuns = nodesSniffer.runs.get();
assertEquals(0, totalRuns);
int setNodesRuns = totalRuns - nodesSniffer.failures.get() - nodesSniffer.emptyList.get();
verify(restClient, times(setNodesRuns)).setNodes(anyCollectionOf(Node.class));
verify(restClient, times(setNodesRuns)).setNodes(anyCollection());
verifyNoMoreInteractions(restClient);
}

Expand Down Expand Up @@ -414,7 +414,7 @@ public void run() {

assertEquals(completedTasks, nodesSniffer.runs.get());
int setNodesRuns = nodesSniffer.runs.get() - nodesSniffer.failures.get() - nodesSniffer.emptyList.get();
verify(restClient, times(setNodesRuns)).setNodes(anyCollectionOf(Node.class));
verify(restClient, times(setNodesRuns)).setNodes(anyCollection());
verifyNoMoreInteractions(restClient);
} finally {
executor.shutdown();
Expand Down
4 changes: 2 additions & 2 deletions client/test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ dependencies {
api "org.hamcrest:hamcrest:${versions.hamcrest}"

// mockito
api 'org.mockito:mockito-core:3.12.4'
api 'net.bytebuddy:byte-buddy:1.11.13'
api 'org.mockito:mockito-core:4.0.0'
api 'net.bytebuddy:byte-buddy:1.11.19'
api 'org.objenesis:objenesis:3.2'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyZeroInteractions;

public class MatcherWatchdogTests extends ESTestCase {

Expand Down Expand Up @@ -77,7 +76,7 @@ public void testIdleIfNothingRegistered() throws Exception {
(delay, command) -> threadPool.schedule(command, delay, TimeUnit.MILLISECONDS)
);
// Periodic action is not scheduled because no thread is registered
verifyZeroInteractions(threadPool);
verifyNoMoreInteractions(threadPool);
CompletableFuture<Runnable> commandFuture = new CompletableFuture<>();
// Periodic action is scheduled because a thread is registered
doAnswer(invocationOnMock -> {
Expand All @@ -92,7 +91,7 @@ public void testIdleIfNothingRegistered() throws Exception {
watchdog.unregister(matcher);
command.run();
// Periodic action is not scheduled again because no thread is registered
verifyZeroInteractions(threadPool);
verifyNoMoreInteractions(threadPool);
watchdog.register(matcher);
Thread otherThread = new Thread(() -> {
Matcher otherMatcher = mock(Matcher.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public void testQueueWriteWhenNotRunning() throws Exception {
selector.close();
selector.queueWrite(new FlushReadyWrite(channelContext, buffers, listener));
});
verify(listener).accept(isNull(Void.class), any(ClosedSelectorException.class));
verify(listener).accept(isNull(), any(ClosedSelectorException.class));
}

public void testQueueWriteChannelIsClosed() throws Exception {
Expand All @@ -312,7 +312,7 @@ public void testQueueWriteChannelIsClosed() throws Exception {
selector.preSelect();

verify(channelContext, times(0)).queueWriteOperation(writeOperation);
verify(listener).accept(isNull(Void.class), any(ClosedChannelException.class));
verify(listener).accept(isNull(), any(ClosedChannelException.class));
}

public void testQueueWriteChannelIsUnregistered() throws Exception {
Expand All @@ -323,7 +323,7 @@ public void testQueueWriteChannelIsUnregistered() throws Exception {
selector.preSelect();

verify(channelContext, times(0)).queueWriteOperation(writeOperation);
verify(listener).accept(isNull(Void.class), any(IllegalStateException.class));
verify(listener).accept(isNull(), any(IllegalStateException.class));
}

public void testQueueWriteSuccessful() throws Exception {
Expand Down Expand Up @@ -484,7 +484,7 @@ public void testCleanup() throws Exception {

selector.cleanupAndCloseChannels();

verify(listener).accept(isNull(Void.class), any(ClosedSelectorException.class));
verify(listener).accept(isNull(), any(ClosedSelectorException.class));
verify(eventHandler).handleClose(channelContext);
verify(eventHandler).handleClose(unregisteredContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public void testWriteFailsIfClosing() {
ByteBuffer[] buffers = { ByteBuffer.wrap(createMessage(10)) };
context.sendMessage(buffers, listener);

verify(listener).accept(isNull(Void.class), any(ClosedChannelException.class));
verify(listener).accept(isNull(), any(ClosedChannelException.class));
}

public void testSendMessageFromDifferentThreadIsQueuedWithSelector() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import java.util.Collections;

import static org.hamcrest.Matchers.equalTo;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyObject;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -46,7 +46,7 @@ public void setUp() throws Exception {

IndexNumericFieldData fieldData = mock(IndexNumericFieldData.class);
when(fieldData.getFieldName()).thenReturn("field");
when(fieldData.load(anyObject())).thenReturn(atomicFieldData);
when(fieldData.load(any())).thenReturn(atomicFieldData);

service = new ExpressionScriptEngine();
lookup = new SearchLookup(field -> field.equals("field") ? fieldType : null, (ignored, lookup) -> fieldData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import java.text.ParseException;
import java.util.Collections;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyObject;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -47,7 +47,7 @@ public void setUp() throws Exception {

IndexNumericFieldData fieldData = mock(IndexNumericFieldData.class);
when(fieldData.getFieldName()).thenReturn("field");
when(fieldData.load(anyObject())).thenReturn(atomicFieldData);
when(fieldData.load(any())).thenReturn(atomicFieldData);

service = new ExpressionScriptEngine();
lookup = new SearchLookup(field -> field.equals("field") ? fieldType : null, (ignored, lookup) -> fieldData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import java.text.ParseException;
import java.util.Collections;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyObject;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand All @@ -46,7 +46,7 @@ public void setUp() throws Exception {

IndexNumericFieldData fieldData = mock(IndexNumericFieldData.class);
when(fieldData.getFieldName()).thenReturn("field");
when(fieldData.load(anyObject())).thenReturn(atomicFieldData);
when(fieldData.load(any())).thenReturn(atomicFieldData);

service = new ExpressionScriptEngine();
lookup = new SearchLookup(field -> field.equals("field") ? fieldType : null, (ignored, lookup) -> fieldData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

public class MetadataRolloverServiceTests extends ESTestCase {
Expand Down Expand Up @@ -843,8 +843,8 @@ public void testValidation() throws Exception {
assertSame(rolloverResult.clusterState, clusterState);

verify(createIndexService).validateIndexName(any(), same(clusterState));
verifyZeroInteractions(createIndexService);
verifyZeroInteractions(metadataIndexAliasesService);
verifyNoMoreInteractions(createIndexService);
verifyNoMoreInteractions(metadataIndexAliasesService);

reset(createIndexService);
doThrow(new InvalidIndexNameException("test", "invalid")).when(createIndexService).validateIndexName(any(), any());
Expand All @@ -863,8 +863,8 @@ public void testValidation() throws Exception {
);

verify(createIndexService).validateIndexName(any(), same(clusterState));
verifyZeroInteractions(createIndexService);
verifyZeroInteractions(metadataIndexAliasesService);
verifyNoMoreInteractions(createIndexService);
verifyNoMoreInteractions(metadataIndexAliasesService);
}

public void testRolloverClusterStateForDataStreamNoTemplate() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

public class TransportBulkActionIngestTests extends ESTestCase {
Expand Down Expand Up @@ -254,7 +254,7 @@ public void testIngestSkipped() throws Exception {
bulkRequest.add(indexRequest);
action.execute(null, bulkRequest, ActionListener.wrap(response -> {}, exception -> { throw new AssertionError(exception); }));
assertTrue(action.isExecuted);
verifyZeroInteractions(ingestService);
verifyNoMoreInteractions(ingestService);
}

public void testSingleItemBulkActionIngestSkipped() throws Exception {
Expand All @@ -266,7 +266,7 @@ public void testSingleItemBulkActionIngestSkipped() throws Exception {
ActionListener.wrap(response -> {}, exception -> { throw new AssertionError(exception); })
);
assertTrue(action.isExecuted);
verifyZeroInteractions(ingestService);
verifyNoMoreInteractions(ingestService);
}

public void testIngestLocal() throws Exception {
Expand Down Expand Up @@ -314,7 +314,7 @@ public void testIngestLocal() throws Exception {
completionHandler.getValue().accept(DUMMY_WRITE_THREAD, null);
assertTrue(action.isExecuted);
assertFalse(responseCalled.get()); // listener would only be called by real index action, not our mocked one
verifyZeroInteractions(transportService);
verifyNoMoreInteractions(transportService);
}

public void testSingleItemBulkActionIngestLocal() throws Exception {
Expand Down Expand Up @@ -349,7 +349,7 @@ public void testSingleItemBulkActionIngestLocal() throws Exception {
completionHandler.getValue().accept(DUMMY_WRITE_THREAD, null);
assertTrue(action.isExecuted);
assertFalse(responseCalled.get()); // listener would only be called by real index action, not our mocked one
verifyZeroInteractions(transportService);
verifyNoMoreInteractions(transportService);
}

public void testIngestSystemLocal() throws Exception {
Expand Down Expand Up @@ -397,7 +397,7 @@ public void testIngestSystemLocal() throws Exception {
completionHandler.getValue().accept(DUMMY_WRITE_THREAD, null);
assertTrue(action.isExecuted);
assertFalse(responseCalled.get()); // listener would only be called by real index action, not our mocked one
verifyZeroInteractions(transportService);
verifyNoMoreInteractions(transportService);
}

public void testIngestForward() throws Exception {
Expand Down Expand Up @@ -558,7 +558,7 @@ private void validatePipelineWithBulkUpsert(@Nullable String indexRequestIndexNa
completionHandler.getValue().accept(DUMMY_WRITE_THREAD, null);
assertTrue(action.isExecuted);
assertFalse(responseCalled.get()); // listener would only be called by real index action, not our mocked one
verifyZeroInteractions(transportService);
verifyNoMoreInteractions(transportService);
}

public void testDoExecuteCalledTwiceCorrectly() throws Exception {
Expand Down Expand Up @@ -598,7 +598,7 @@ public void testDoExecuteCalledTwiceCorrectly() throws Exception {
assertTrue(action.isExecuted);
assertTrue(action.indexCreated); // now the index is created since we skipped the ingest node path.
assertFalse(responseCalled.get()); // listener would only be called by real index action, not our mocked one
verifyZeroInteractions(transportService);
verifyNoMoreInteractions(transportService);
}

public void testNotFindDefaultPipelineFromTemplateMatches() {
Expand All @@ -612,7 +612,7 @@ public void testNotFindDefaultPipelineFromTemplateMatches() {
failureCalled.set(true);
}));
assertEquals(IngestService.NOOP_PIPELINE_NAME, indexRequest.getPipeline());
verifyZeroInteractions(ingestService);
verifyNoMoreInteractions(ingestService);

}

Expand Down Expand Up @@ -749,6 +749,6 @@ private void validateDefaultPipeline(IndexRequest indexRequest) {
completionHandler.getValue().accept(DUMMY_WRITE_THREAD, null);
assertTrue(action.isExecuted);
assertFalse(responseCalled.get()); // listener would only be called by real index action, not our mocked one
verifyZeroInteractions(transportService);
verifyNoMoreInteractions(transportService);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.ArgumentMatchers.anyObject;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
Expand Down Expand Up @@ -144,7 +144,7 @@ public void testResyncDoesNotBlockOnPrimaryAction() throws Exception {
acquiredPermits.incrementAndGet();
callback.onResponse(acquiredPermits::decrementAndGet);
return null;
}).when(indexShard).acquirePrimaryOperationPermit(anyActionListener(), anyString(), anyObject(), eq(true));
}).when(indexShard).acquirePrimaryOperationPermit(anyActionListener(), anyString(), any(), eq(true));
when(indexShard.getReplicationGroup()).thenReturn(
new ReplicationGroup(
shardRoutingTable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyObject;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
Expand Down Expand Up @@ -939,7 +938,7 @@ public void testSeqNoIsSetOnPrimary() {
ActionListener<Releasable> argument = (ActionListener<Releasable>) invocation.getArguments()[0];
argument.onResponse(count::decrementAndGet);
return null;
}).when(shard).acquirePrimaryOperationPermit(any(), anyString(), anyObject(), eq(forceExecute));
}).when(shard).acquirePrimaryOperationPermit(any(), anyString(), any(), eq(forceExecute));
when(shard.getActiveOperationsCount()).thenAnswer(i -> count.get());

final IndexService indexService = mock(IndexService.class);
Expand Down Expand Up @@ -1569,7 +1568,7 @@ private IndexShard mockIndexShard(ShardId shardId, ClusterService clusterService
callback.onFailure(new ShardNotInPrimaryModeException(shardId, IndexShardState.STARTED));
}
return null;
}).when(indexShard).acquirePrimaryOperationPermit(any(ActionListener.class), anyString(), anyObject(), eq(forceExecute));
}).when(indexShard).acquirePrimaryOperationPermit(any(ActionListener.class), anyString(), any(), eq(forceExecute));
doAnswer(invocation -> {
long term = (Long) invocation.getArguments()[0];
ActionListener<Releasable> callback = (ActionListener<Releasable>) invocation.getArguments()[3];
Expand All @@ -1582,8 +1581,7 @@ private IndexShard mockIndexShard(ShardId shardId, ClusterService clusterService
count.incrementAndGet();
callback.onResponse(count::decrementAndGet);
return null;
}).when(indexShard)
.acquireReplicaOperationPermit(anyLong(), anyLong(), anyLong(), any(ActionListener.class), anyString(), anyObject());
}).when(indexShard).acquireReplicaOperationPermit(anyLong(), anyLong(), anyLong(), any(ActionListener.class), anyString(), any());
when(indexShard.getActiveOperationsCount()).thenAnswer(i -> count.get());

when(indexShard.routingEntry()).thenAnswer(invocationOnMock -> {
Expand Down
Loading

0 comments on commit 99b6e3e

Please sign in to comment.