LoginSignup
0
0

More than 1 year has passed since last update.

Firebase Cloud Messagingのメッセージのテストコードを書く(Java)

Last updated at Posted at 2022-11-24

方法

ApiClientUtils.getDefaultJsonFactory() を使用すると、MessageクラスをJSON文字列に変換できる。

// 実際値のMessageクラス
Message actual = Message.builder()
        .setNotification(Notification.builder().setBody("message_text").setTitle("").build())
        .putData("id", "data")
        .setToken("FCM_TOKEN_XXXXX")
        .build();

// 期待値のJSON文字列
String expected = """
        {
            "data": {
                "id": "data"
            },
            "notification": {
                "body": "message_text",
                "title": ""
            },
            "token": "FCM_TOKEN_XXXXX"
        }
        """.replaceAll("[\s\r\n]", "");

// MessageをJSON文字列へ
JsonFactory jsonFactory = ApiClientUtils.getDefaultJsonFactory();
assertThat(jsonFactory.toString(actual)).isEqualTo(expected);

元ネタ

公式のテストコードで使用されている assertJsonEquals() の実装がそうなってる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0