[FR] Relax encapsulation for Firebase Messaging exception and Messages payload to enable testability
@lahirumaramba y travaille déjà.
Depuis le 8/4/2025.
Évaluation
Cette issue n'a pas encore été évaluée.
Description
Currently, unit testing for integration with Firebase Messaging is very challenging. The encapsulation imposed by the final and visibility of the method makes it untestable. On top of this, there is no emulator for Firebase messaging which could help with testing integration with a more production-like environment.
- I want to emulate exceptions on FCM Messaging like UNREGISTERED, INVALID_ARGUMENT when calling
com.google.firebase.messaging.FirebaseMessaging#send(com.google.firebase.messaging.Message)to test my error handling implementation e.g deleting invalid token
- com.google.firebase.messaging.FirebaseMessagingException is final thus mocking with popular libraries like Mockito is more difficult. We need to enable extra feature flags in Mockito to allow for mocking the class
- It's impossible to create an instance of an exception with FCM Messaging exception as factory methods and constructors are package protected
com.google.firebase.messaging.FirebaseMessagingException#withMessagingErrorCode
- I want to unit test if a message with a given payalod was sent to FCM
Currently FCM payload represented withcom.google.firebase.messaging.Messageis not accessible to third parties via getters.
- Assuming I want to test if a proper payload was sent via
com.google.firebase.messaging.FirebaseMessaging#send(com.google.firebase.messaging.Message)to FCM I would like to mock com.google.firebase.messaging.FirebaseMessaging and validate thatcom.google.firebase.messaging.FirebaseMessaging#send(com.google.firebase.messaging.Message)method was invoked with givendataandtoken
In this case, developers used @VisibleForTesting for their purposes for testing while they totally forgot about developers who use their library and can not access the payload of the message. On top of this, there is no provided reasonable toString to make any assertions based on this.
Describe the solution you'd like
Exceptions testing
- Option 1
Relax encapsulation for com.google.firebase.messaging.FirebaseMessagingException by deleting "final" on class and makeerrorCodeprotected - Option 2
Make factory method publiccom.google.firebase.messaging.FirebaseMessagingException#withMessagingErrorCode
Messages testing
- Option 1
Make getter public forcom.google.firebase.messaging.Message#Messageto allow accessing payload of FCM message - Option 2
Provide reasonable toString to allow asserting on the toString method
Describe alternatives you've considered
- Provide emulator for FCM which allows emulating different scenarios including errors
- Provide dedicated implementations for testing purposes, open for all users
Additional context
Too aggressive encapsulation in my opinion is harmful. Developers should balance encapsulation and testability, taking into account the users of their libraries not only their own goals to protect their code with all means available in Java which are not perfect
- Langage dominant
- Java
- Étoiles
- 620
- Forks
- 305
- Merge moyen
- 4 j 9 h
- PR mergées (30 j)
- 4
Guide de contribution
Ouvrir 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 firebase/firebase-admin-java
-
Difficulté 4/5 3-5 jours Accessibilité débutants 48/100
firebase/firebase-admin-java#1231 ·
-
api: core
firebase/firebase-admin-java#1198 · 3 commentaires · 1 personne assignée ·
-
[BUG] RemoteConfig unit tests fail in non-English (e.g., Korean) Locales due to Date Parsing Ouverteapi: remoteconfig
firebase/firebase-admin-java#1196 · 1 commentaire · 2 personnes assignées ·
-
Difficulté 3/5 1-2 jours Accessibilité débutants 45/100
firebase/firebase-admin-java#1192 · 3 commentaires ·
-
needs-triage type: feature request
Difficulté 3/5 1-2 jours Accessibilité débutants 45/100
firebase/firebase-admin-java#1149 · 1 commentaire ·
Toutes les issues de firebase/firebase-admin-java
Issues similaires
-
awaiting triage bug Causes friction Hop Gui P1 P2 Transforms
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
-
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
apache/flink-agents#1152 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
-
Difficulté 2/5 1-3 heures Accessibilité débutants 70/100
jenkinsci/blueocean-plugin#5417 ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 75/100
objectionary/eo-graphs#75 ·