Help us understand the problem. What is going on with this article?

Remote Configのデフォルト値を追加し忘れる問題

テストで解決。

import android.content.Context
import android.content.res.XmlResourceParser
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.xmlpull.v1.XmlPullParser

@RunWith(AndroidJUnit4::class)
class RemoteConfigTest {
    private lateinit var defaultXmlParser: XmlResourceParser

    @Before
    fun setUp() {
        val context: Context = InstrumentationRegistry.getInstrumentation().targetContext
        defaultXmlParser = context.resources.getXml(R.xml.remote_config_defaults)
    }

    @Test
    fun assertAllKeysHaveDefaultValues() {
        // デフォルト値が定義されているkeyを取り出す
        val keysWithDefaults: MutableSet<String> = mutableSetOf()
        while (defaultXmlParser.eventType != XmlPullParser.END_DOCUMENT) {
            if (defaultXmlParser.eventType == XmlPullParser.START_TAG && defaultXmlParser.name == "key") {
                defaultXmlParser.next()

                keysWithDefaults.add(defaultXmlParser.text)
            }

            defaultXmlParser.next()
        }

        // デフォルト値が定義されていないkeyを取り出す
        val keysWithoutDefaults: Set<String> = RemoteConfigManager.keys() - keysWithDefaults

        // デフォルト値が定義されていないkeyがなければ終了
        if (keysWithoutDefaults.isEmpty()) return

        throw IllegalStateException(
            """keyに対応するデフォルト値が存在しない:${keysWithoutDefaults.joinToString(separator = ", ")}"""
        )
    }
}

XmlPullParserの代わりにXPathを使うともっとすっきり書けるかもしれない。。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした