1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ThreeTenABPに依存したクラスをテストしたい(メモ)

Posted at

例えばJSON→ZonedDateTimeを持つPOJOへの変換の確認など。
デシリアライザにはGsonを使う想定です。

build.gradle
android {
    // (省略)
    testVariants.all { variant ->
        variant.getCompileConfiguration().exclude group: 'com.jakewharton.threetenabp', module: 'threetenabp'
        variant.getRuntimeConfiguration().exclude group: 'com.jakewharton.threetenabp', module: 'threetenabp'
    }
}

dependencies {
    // (省略)
    implementation "com.jakewharton.threetenabp:threetenabp:$threetenabp_version"
    androidTestImplementation("org.threeten:threetenbp:$threetenbp_version") {
        exclude group: 'com.jakewharton.threetenabp', module: 'threetenabp'
    }
}
String→ZonedDateTimeのJsonDeserializerの例
class IsoZonedDateTimeDeserializer : JsonDeserializer<ZonedDateTime> {
    @Throws(JsonParseException::class)
    override fun deserialize(json: JsonElement?, typeOfT: Type?, context: JsonDeserializationContext?): ZonedDateTime {
        val jsonPrimitive = json?.asJsonPrimitive
        try {
            if (jsonPrimitive is JsonPrimitive && jsonPrimitive.isString) {
                return ZonedDateTime.parse(jsonPrimitive.asString, DateTimeFormatter.ISO_ZONED_DATE_TIME)
            }
        } catch (e: RuntimeException) {
            throw JsonParseException("Unable to parse ZonedDateTime", e)
        }
        throw JsonParseException("Unable to parse ZonedDateTime")
    }
}
1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?