Skip to content
Permalink
Browse files
deps: upgrade Mockito to version 4.x (#1498)
  • Loading branch information
olavloite committed Oct 27, 2021
1 parent 08dd040 commit 09bd56157827119586fd3e0a1ee056bb793d08e3
Showing with 166 additions and 138 deletions.
  1. +4 −9 google-cloud-spanner/pom.xml
  2. +1 −1 google-cloud-spanner/src/test/java/com/google/cloud/spanner/AsyncRunnerImplTest.java
  3. +1 −1 google-cloud-spanner/src/test/java/com/google/cloud/spanner/AsyncTransactionManagerImplTest.java
  4. +35 −0 google-cloud-spanner/src/test/java/com/google/cloud/spanner/JavaVersionUtil.java
  5. +2 −2 google-cloud-spanner/src/test/java/com/google/cloud/spanner/PartitionedDmlTransactionTest.java
  6. +28 −3 google-cloud-spanner/src/test/java/com/google/cloud/spanner/ResumableStreamIteratorTest.java
  7. +7 −7 google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionClientTest.java
  8. +1 −5 google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionImplTest.java
  9. +3 −3 google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionPoolMaintainerTest.java
  10. +3 −3 google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionPoolStressTest.java
  11. +29 −29 google-cloud-spanner/src/test/java/com/google/cloud/spanner/SessionPoolTest.java
  12. +3 −14 google-cloud-spanner/src/test/java/com/google/cloud/spanner/SpanTest.java
  13. +2 −2 google-cloud-spanner/src/test/java/com/google/cloud/spanner/TransactionContextImplTest.java
  14. +1 −1 google-cloud-spanner/src/test/java/com/google/cloud/spanner/TransactionManagerImplTest.java
  15. +1 −1 google-cloud-spanner/src/test/java/com/google/cloud/spanner/TransactionRunnerImplTest.java
  16. +1 −1 google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/AutocommitDmlModeTest.java
  17. +10 −11 google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/ConnectionImplTest.java
  18. +1 −1 ...ud-spanner/src/test/java/com/google/cloud/spanner/connection/ConnectionStatementExecutorTest.java
  19. +1 −1 ...er/src/test/java/com/google/cloud/spanner/connection/ConnectionStatementWithOneParameterTest.java
  20. +13 −14 google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/DdlBatchTest.java
  21. +4 −5 google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/DdlClientTest.java
  22. +2 −2 google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/DmlBatchTest.java
  23. +6 −14 google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/EmulatorUtilTest.java
  24. +3 −4 google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/ReadOnlyStalenessTest.java
  25. +1 −1 google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/ReadWriteTransactionTest.java
  26. +3 −3 google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/SingleUseTransactionTest.java
@@ -282,15 +282,10 @@
<artifactId>truth</artifactId>
<scope>test</scope>
</dependency>
<dependency><groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<exclusions>
<exclusion>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
</exclusion>
</exclusions>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
@@ -21,7 +21,7 @@
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@@ -26,7 +26,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class AsyncTransactionManagerImplTest {
@@ -0,0 +1,35 @@
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.spanner;

/** Util class for getting the Java version the tests are executed on. */
public class JavaVersionUtil {

/** Returns the major Java version (e.g. 8, 11, 17) */
public static int getJavaMajorVersion() {
String version = System.getProperty("java.version");
if (version.startsWith("1.")) {
version = version.substring(2, 3);
} else {
int dot = version.indexOf(".");
if (dot != -1) {
version = version.substring(0, dot);
}
}
return Integer.parseInt(version);
}
}
@@ -19,8 +19,8 @@
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyMap;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyMap;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@@ -23,6 +23,7 @@
import static org.mockito.Mockito.verify;

import com.google.api.client.util.BackOff;
import com.google.cloud.spanner.AbstractResultSet.ResumableStreamIterator;
import com.google.common.collect.AbstractIterator;
import com.google.common.collect.Lists;
import com.google.protobuf.ByteString;
@@ -37,18 +38,19 @@
import io.opencensus.trace.EndSpanOptions;
import io.opencensus.trace.Span;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mockito;
import org.mockito.internal.util.reflection.Whitebox;

/** Unit tests for {@link AbstractResultSet.ResumableStreamIterator}. */
@RunWith(JUnit4.class)
@@ -156,8 +158,12 @@ public void simple() {

@Test
public void closedSpan() {
Assume.assumeTrue(
"This test is only supported on JDK11 and lower",
JavaVersionUtil.getJavaMajorVersion() < 12);

Span span = mock(Span.class);
Whitebox.setInternalState(this.resumableStreamIterator, "span", span);
setInternalState(ResumableStreamIterator.class, this.resumableStreamIterator, "span", span);

ResultSetStream s1 = Mockito.mock(ResultSetStream.class);
Mockito.when(starter.startStream(null)).thenReturn(new ResultSetIterator(s1));
@@ -236,9 +242,14 @@ public void restartWithHoldBackMidStream() {

@Test
public void retryableErrorWithoutRetryInfo() throws IOException {
Assume.assumeTrue(
"This test is only supported on JDK11 and lower",
JavaVersionUtil.getJavaMajorVersion() < 12);

BackOff backOff = mock(BackOff.class);
Mockito.when(backOff.nextBackOffMillis()).thenReturn(1L);
Whitebox.setInternalState(this.resumableStreamIterator, "backOff", backOff);
setInternalState(
ResumableStreamIterator.class, this.resumableStreamIterator, "backOff", backOff);

ResultSetStream s1 = Mockito.mock(ResultSetStream.class);
Mockito.when(starter.startStream(null)).thenReturn(new ResultSetIterator(s1));
@@ -446,4 +457,18 @@ static List<String> consumeAtMost(int n, Iterator<PartialResultSet> iterator) {
}
return r;
}

/**
* Sets a private static final field to a specific value. This is only supported on Java11 and
* lower.
*/
private static void setInternalState(Class<?> c, Object target, String field, Object value) {
try {
Field f = c.getDeclaredField(field);
f.setAccessible(true);
f.set(target, value);
} catch (Exception e) {
throw new RuntimeException("Unable to set internal state on a private field.", e);
}
}
}
@@ -149,10 +149,10 @@ public void batchCreateAndCloseSessions() {
Mockito.eq(dbName), Mockito.anyInt(), Mockito.eq(labels), Mockito.anyMap()))
.then(
invocation -> {
Map<Option, Object> options = invocation.getArgumentAt(3, Map.class);
Map<Option, Object> options = invocation.getArgument(3, Map.class);
Long channelHint = (Long) options.get(Option.CHANNEL_HINT);
usedChannels.add(channelHint);
int sessionCount = invocation.getArgumentAt(1, Integer.class);
int sessionCount = invocation.getArgument(1, Integer.class);
List<com.google.spanner.v1.Session> res = new ArrayList<>();
for (int i = 1; i <= sessionCount; i++) {
res.add(
@@ -209,10 +209,10 @@ public void batchCreateSessionsDistributesMultipleRequestsOverChannels() {
Mockito.eq(dbName), Mockito.anyInt(), Mockito.eq(labels), Mockito.anyMap()))
.then(
invocation -> {
Map<Option, Object> options = invocation.getArgumentAt(3, Map.class);
Map<Option, Object> options = invocation.getArgument(3, Map.class);
Long channelHint = (Long) options.get(Option.CHANNEL_HINT);
usedChannelHints.add(channelHint);
int sessionCount = invocation.getArgumentAt(1, Integer.class);
int sessionCount = invocation.getArgument(1, Integer.class);
List<com.google.spanner.v1.Session> res = new ArrayList<>();
for (int i = 1; i <= sessionCount; i++) {
res.add(
@@ -290,13 +290,13 @@ public void batchCreateSessionsWithExceptions() {
Mockito.eq(dbName), Mockito.anyInt(), Mockito.anyMap(), Mockito.anyMap()))
.then(
invocation -> {
Map<Option, Object> options = invocation.getArgumentAt(3, Map.class);
Map<Option, Object> options = invocation.getArgument(3, Map.class);
Long channelHint = (Long) options.get(Option.CHANNEL_HINT);
if (errorOnChannels.contains(channelHint)) {
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.RESOURCE_EXHAUSTED, "could not create any more sessions");
} else {
int sessionCount = invocation.getArgumentAt(1, Integer.class);
int sessionCount = invocation.getArgument(1, Integer.class);
List<com.google.spanner.v1.Session> res = new ArrayList<>();
for (int i = 1; i <= sessionCount; i++) {
res.add(
@@ -354,7 +354,7 @@ public void batchCreateSessionsServerReturnsLessSessionsPerBatch() {
Mockito.eq(dbName), Mockito.anyInt(), Mockito.anyMap(), Mockito.anyMap()))
.then(
invocation -> {
int sessionCount = invocation.getArgumentAt(1, Integer.class);
int sessionCount = invocation.getArgument(1, Integer.class);
List<com.google.spanner.v1.Session> res = new ArrayList<>();
for (int i = 1; i <= Math.min(MAX_SESSIONS_PER_BATCH, sessionCount); i++) {
res.add(
@@ -92,11 +92,7 @@ public void setUp() {
DatabaseId db = DatabaseId.of(dbName);

Session sessionProto = Session.newBuilder().setName(sessionName).build();
Mockito.when(
rpc.createSession(
Mockito.eq(dbName),
Mockito.anyMapOf(String.class, String.class),
optionsCaptor.capture()))
Mockito.when(rpc.createSession(Mockito.eq(dbName), Mockito.anyMap(), optionsCaptor.capture()))
.thenReturn(sessionProto);
Transaction txn = Transaction.newBuilder().setId(ByteString.copyFromUtf8("TEST")).build();
Mockito.when(
@@ -17,7 +17,7 @@
package com.google.cloud.spanner;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -77,9 +77,9 @@ private void setupMockSessionCreation() {
invocation -> {
executor.submit(
() -> {
int sessionCount = invocation.getArgumentAt(0, Integer.class);
int sessionCount = invocation.getArgument(0, Integer.class);
SessionConsumerImpl consumer =
invocation.getArgumentAt(2, SessionConsumerImpl.class);
invocation.getArgument(2, SessionConsumerImpl.class);
for (int i = 0; i < sessionCount; i++) {
consumer.onSessionReady(setupMockSession(mockSession()));
}
@@ -17,7 +17,7 @@
package com.google.cloud.spanner;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -96,7 +96,7 @@ private void setupSpanner(DatabaseId db) {
invocation -> {
createExecutor.submit(
() -> {
int sessionCount = invocation.getArgumentAt(0, Integer.class);
int sessionCount = invocation.getArgument(0, Integer.class);
for (int s = 0; s < sessionCount; s++) {
SessionImpl session;
synchronized (lock) {
@@ -108,7 +108,7 @@ private void setupSpanner(DatabaseId db) {
}
}
SessionConsumerImpl consumer =
invocation.getArgumentAt(2, SessionConsumerImpl.class);
invocation.getArgument(2, SessionConsumerImpl.class);
consumer.onSessionReady(session);
}
});

0 comments on commit 09bd561

Please sign in to comment.