Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

[FR] Relax encapsulation for Firebase Messaging exception and Messages payload to enable testability

未关闭
#695 9 条评论 11 个 reaction 已指派 1 人 在 GitHub 查看

@lahirumaramba 已经在做这个了。

开始于 2025年4月8日。

评估

这个 Issue 还没有评估数据。

描述

api: messaging type: feature request

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.

  1. 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
  1. I want to unit test if a message with a given payalod was sent to FCM
    Currently FCM payload represented with com.google.firebase.messaging.Message is 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 that com.google.firebase.messaging.FirebaseMessaging#send(com.google.firebase.messaging.Message) method was invoked with given data and token

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 make errorCode protected
  • Option 2
    Make factory method public com.google.firebase.messaging.FirebaseMessagingException#withMessagingErrorCode

Messages testing

  • Option 1
    Make getter public for com.google.firebase.messaging.Message#Message to 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

主要语言
Java
星标
620
派生
305
平均合并
4 天 9 小时
30 天内合并 PR
4

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

firebase/firebase-admin-java 的其他 Issue

查看 firebase/firebase-admin-java 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。