[Testing] Mockito fails with ArrayStoreException on JDK 8 due to JSpecify TYPE_USE annotations
Les mainteneurs répondent en général sous 1 jour
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 35/100
- Type d'issue
- Bug
- Clarté
- À clarifier
- Activité
- Calme
- Stack technique
- java
- Domaine
- testing-qa
Piste de recherche
Commencez par exécuter la phase de test de Maven sur JDK 8 avec une classe Stub ou Client générée utilisant des annotations JSpecify, ainsi qu’un mock Mockito standard. Comparez le mock basé sur la déclaration qui échoue avec Mockito.mock(..., Mockito.withSettings().withoutAnnotations()). L’issue ne nomme aucun fichier cible ni aucun critère d’acceptation ; la correction attendue et la définition of done doivent donc être clarifiées.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Description
Mockito tests running on Java 8 (JDK 1.8) crash with an ArrayStoreException / EnumConstantNotPresentExceptionProxy when attempting to mock generated Stub or Client classes that contain JSpecify annotations (@NullMarked or @Nullable).
Because JSpecify uses Type-Use (TYPE_USE) annotations, it triggers a known limitation in standard JDK 8 Java Reflection API. Standard JVM annotation parsers throw an ArrayStoreException when bytecode scanners (like ByteBuddy, underneath Mockito) introspect parameterized types, arrays, or generic descriptors holding Type-Use annotations.
Workaround
Handwritten tests aiming to maintain backwards compatibility with Java 8 while using JSpecify must explicitly disable annotation processing on mocked generated classes. Instead of standard @Mock Declaration annotations, instantiations must use:
Mockito.mock(MyStub.class, Mockito.withSettings().withoutAnnotations());.
This workaround has been successfully applied in the following integration PRs:
Reproducer
Steps to reproduce:
- Generate GAPIC veneers/stubs annotated with JSpecify @NullMarked or @Nullable (Type-Use).
- Write a handwritten unit test using Java 8 runtime.
- Instantiate standard Mockito mock for the generated class: MetricServiceStub mockStub = Mockito.mock(MetricServiceStub.class); or @Mock private MetricServiceStub mockStub;.
- Run tests and observe ArrayStoreException crash.
Code Snippets:
Failing Snippet (Standard/Declaration):
@Mock private MetricServiceStub mockMetricServiceStub; // Crashes on JDK 8
Bypass/Working Snippet (Programmatic without annotations):
private MetricServiceStub mockMetricServiceStub;
@Before
public void setUp() {
// Bypasses reflection parser crash
mockMetricServiceStub = Mockito.mock(
MetricServiceStub.class,
Mockito.withSettings().withoutAnnotations()
);
}
Logs and Stack Trace
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class com.google.cloud.monitoring.v3.stub.MetricServiceStub.
Mockito can only mock non-private & non-final classes, but the root cause of this error might be different.
...
Caused by: java.lang.ArrayStoreException: sun.reflect.annotation.EnumConstantNotPresentExceptionProxy
at java.lang.Class.createAnnotationData(Class.java:3521)
at java.lang.Class.annotationData(Class.java:3510)
at java.lang.Class.getDeclaredAnnotations(Class.java:3477)
Behavior
When did the issue begin? Began following PR integrations introducing JSpecify Type-Use annotations (@NullMarked, @Nullable) to generated surfaces.
Is this behavior flaky? No, it is consistently seen during the Maven Testing Phase running specifically on JDK 1.8.
- Langage dominant
- Java
- Étoiles
- 2.1k
- Forks
- 1.2k
- Merge moyen
- 2 j 14 h
- PR mergées (30 j)
- 192
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Aucun modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de googleapis/google-cloud-java
-
[java-core] Timestamp.of(java.sql.Timestamp) is one second late for a time less than a second before the epochPeut-être pris @Vcode2407 l’a pris il y a 4 jours. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
googleapis/google-cloud-java#14636 ·
Les mainteneurs répondent en général sous 1 jour
-
[java-firestore] UserDataConverter encodes a pre-epoch Date with negative nanos, so the write is refusedPeut-être pris @wu-hui l’a pris il y a 23 jours. Ouvertepriority: p2 type: bug
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
googleapis/google-cloud-java#14635 · 1 personne assignée ·
Les mainteneurs répondent en général sous 1 jour
-
[java-firestore] UserDataConverter throws IndexOutOfBoundsException on an empty __binary__ valuePeut-être pris @wu-hui l’a pris il y a 23 jours. Ouvertepriority: p2 type: bug
Difficulté 2/5 1-3 heures Accessibilité débutants 74/100
googleapis/google-cloud-java#14634 · 1 commentaire · 1 personne assignée ·
Les mainteneurs répondent en général sous 1 jour
-
[java-firestore] CustomClassMapper.convertBigDecimal calls .abs() on Double, unintentionally flipping negative values to positivePeut-être à nouveau libre @wu-hui l’a pris il y a 45 jours, et aucune pull request n’est ouverte. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 85/100
googleapis/google-cloud-java#14533 · 1 personne assignée ·
Les mainteneurs répondent en général sous 1 jour
-
Github CI failing when trying to install xmllintPeut-être pris @blakeli0 l’a pris il y a 81 jours. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 65/100
googleapis/google-cloud-java#14132 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de googleapis/google-cloud-java
Issues similaires
-
Difficulté 2/5 1-3 heures Accessibilité débutants 80/100
finos/fluxnova-bpm-platform#387 ·
Les mainteneurs répondent en général sous 4 jours
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 88/100
-
Difficulté 2/5 Moins d'une heure Accessibilité débutants 84/100
cpp-best-practices/project_validator#251 ·
Les mainteneurs répondent en général sous 1 jour
-
backend enhancement
Difficulté 2/5 Une demi-journée Accessibilité débutants 70/100
damodharg6/payledger#16 ·
-
[Bug]: An initiative list with no current entry logs an error on every changePeut-être pris @dwizard0 l’a pris aujourd’hui. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
Les mainteneurs répondent en général sous 1 jour