Skip to content

Commit dbc9b14

Browse files
nnicoleemutianf
authored andcommitted
test(spanner): disable Mockito annotation copying for Credentials, ServiceAccountCredentials, and RequestMetadataCallback (googleapis#13861)
### Problem In JSpecify 1.0.0, the `@NullMarked` annotation targets `ElementType.MODULE`. Because `ElementType.MODULE` was introduced in Java 9, reflecting on `@NullMarked` classes under Java 8 (JDK 1.8) throws `EnumConstantNotPresentExceptionProxy` wrapped in an `ArrayStoreException`. In `MutableCredentialsTest` and `SpannerPoolTest`, mock declarations for `Credentials`, `ServiceAccountCredentials`, and `RequestMetadataCallback` were created using the default `mock(Class.class)` without setting `.withoutAnnotations()`. Mockito attempts to copy annotations on the mock subclasses, triggering the reflection crash on the JSpecify `@NullMarked` annotation during Java 8 CI pipelines. ### Solution Replaced default `mock(...)` calls with `mock(..., withSettings().withoutAnnotations())` for: * `Credentials` in `SpannerPoolTest` * `ServiceAccountCredentials` and `RequestMetadataCallback` in `MutableCredentialsTest` This disables annotation copying on these mocked instances and bypasses the Java 8 reflection limitations.
1 parent c21d74d commit dbc9b14

2 files changed

Lines changed: 15 additions & 7 deletions

File tree

java-spanner/google-cloud-spanner/src/test/java/com/google/cloud/spanner/MutableCredentialsTest.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import static org.mockito.Mockito.times;
2727
import static org.mockito.Mockito.verify;
2828
import static org.mockito.Mockito.when;
29+
import static org.mockito.Mockito.withSettings;
2930

3031
import com.google.auth.CredentialTypeForMetrics;
3132
import com.google.auth.RequestMetadataCallback;
@@ -45,10 +46,14 @@
4546

4647
@RunWith(JUnit4.class)
4748
public class MutableCredentialsTest {
48-
ServiceAccountCredentials initialCredentials = mock(ServiceAccountCredentials.class);
49-
ServiceAccountCredentials initialScopedCredentials = mock(ServiceAccountCredentials.class);
50-
ServiceAccountCredentials updatedCredentials = mock(ServiceAccountCredentials.class);
51-
ServiceAccountCredentials updatedScopedCredentials = mock(ServiceAccountCredentials.class);
49+
ServiceAccountCredentials initialCredentials =
50+
mock(ServiceAccountCredentials.class, withSettings().withoutAnnotations());
51+
ServiceAccountCredentials initialScopedCredentials =
52+
mock(ServiceAccountCredentials.class, withSettings().withoutAnnotations());
53+
ServiceAccountCredentials updatedCredentials =
54+
mock(ServiceAccountCredentials.class, withSettings().withoutAnnotations());
55+
ServiceAccountCredentials updatedScopedCredentials =
56+
mock(ServiceAccountCredentials.class, withSettings().withoutAnnotations());
5257
Set<String> scopes = new HashSet<>(Arrays.asList("scope-a", "scope-b"));
5358
Map<String, List<String>> initialMetadata =
5459
Collections.singletonMap("Authorization", Collections.singletonList("v1"));
@@ -70,7 +75,8 @@ public void testCreateMutableCredentials() throws IOException {
7075
MutableCredentials credentials = new MutableCredentials(initialCredentials, scopes);
7176
URI testUri = URI.create("https://spanner.googleapis.com");
7277
Executor executor = mock(Executor.class);
73-
RequestMetadataCallback callback = mock(RequestMetadataCallback.class);
78+
RequestMetadataCallback callback =
79+
mock(RequestMetadataCallback.class, withSettings().withoutAnnotations());
7480

7581
validateInitialDelegatedCredentialsAreSet(credentials, testUri);
7682

@@ -111,7 +117,8 @@ public void testUpdateMutableCredentials() throws IOException {
111117
MutableCredentials credentials = new MutableCredentials(initialCredentials, scopes);
112118
URI testUri = URI.create("https://example.com");
113119
Executor executor = mock(Executor.class);
114-
RequestMetadataCallback callback = mock(RequestMetadataCallback.class);
120+
RequestMetadataCallback callback =
121+
mock(RequestMetadataCallback.class, withSettings().withoutAnnotations());
115122

116123
validateInitialDelegatedCredentialsAreSet(credentials, testUri);
117124

java-spanner/google-cloud-spanner/src/test/java/com/google/cloud/spanner/connection/SpannerPoolTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static org.mockito.Mockito.never;
2525
import static org.mockito.Mockito.verify;
2626
import static org.mockito.Mockito.when;
27+
import static org.mockito.Mockito.withSettings;
2728

2829
import com.google.auth.Credentials;
2930
import com.google.cloud.NoCredentials;
@@ -477,7 +478,7 @@ public void testSpannerPoolKeyEquality() {
477478
.setUri(
478479
"cloudspanner://localhost:9010/projects/p1/instances/i/databases/d"
479480
+ "?minSessions=200;maxSessions=400;numChannels=8;usePlainText=true;userAgent=test-agent")
480-
.setCredentials(mock(Credentials.class))
481+
.setCredentials(mock(Credentials.class, withSettings().withoutAnnotations()))
481482
.build();
482483
// options2 equals the default session pool options, and is therefore equal to ConnectionOptions
483484
// without any session pool configuration.

0 commit comments

Comments
 (0)