首页 > 解决方案 > 即使具有相同的数据,断言也无法比较两个数据类

问题描述

我有两个数据类 - HindiTranslation 和 EnglishTranslation。我正在使用第三个数据类 - HindiAndEnglishTranslation,将它们映射到房间数据库中的一对多关系中。我试图通过断言给定和检索的 HindiAndEnglishTranslation 对象是否相等来测试 Room Dao,但即使两个对象中的数据相同,测试也会失败。

印地语翻译:

@Entity(tableName = "hindi_translations")
data class HindiTranslation(
    @PrimaryKey
    var word: String,
    val hindiTranslation: String,
    val id: Long = randomNumberGenerator()
)

英文翻译:

@Entity(tableName = "english_translations")
data class EnglishTranslation(
    val translations: List<String>,
    val examples: List<String>,
    val usage: String,
    var parentKey: String,
    @PrimaryKey
    var englishId: Long = randomNumberGenerator(),
)

印地语和英语翻译:

data class HindiAndEnglishTranslation(
    @Embedded val hindi: HindiTranslation,
    @Relation(
        parentColumn = "word",
        entityColumn = "parentKey"
    )
    val englishTranslations: List<EnglishTranslation>
)

道测试中的代码:

    @Test
    fun getAllTranslations() = runBlockingTest {
        //  Given - inserting a few hindi and corresponding english translations
        database.getTranslationDao().insertHindiTranslation(hindi1)
        database.getTranslationDao().insertEnglishTranslation(english1)
        database.getTranslationDao().insertHindiTranslation(hindi2)
        database.getTranslationDao().insertEnglishTranslation(english22)
        database.getTranslationDao().insertEnglishTranslation(english32)
        database.getTranslationDao().insertEnglishTranslation(english42)

        //  When - all translations in database are fetched
        val translations = database.getTranslationDao().getAllTranslations()

        //  Then - the loaded translations contain the expected values
        assertThat(translations, equalTo((listOf(hindiAndEnglish1, hindiAndEnglish2))))

    }

我得到的错误:

java.lang.AssertionError: 
Expected: <HindiAndEnglishTranslation(hindi=HindiTranslation(word=hot, hindiTranslation=गरम, id=6050220789869782304), englishTranslations=[EnglishTranslation(translations=[having a high degree of heat or a high temperature, (of food) containing or consisting of pungent spices or peppers which produce a burning sensation when tasted, passionately enthusiastic, eager, or excited], examples=[it was hot inside the hall, a very hot dish cooked with green chili, the idea had been nurtured in his hot imagination], usage=Adjective, parentKey=hot, englishId=133595475665305056)])>
     but: was <HindiAndEnglishTranslation(hindi=HindiTranslation(word=hot, hindiTranslation=गरम, id=6050220789869782304), englishTranslations=[EnglishTranslation(translations=[having a high degree of heat or a high temperature, (of food) containing or consisting of pungent spices or peppers which produce a burning sensation when tasted, passionately enthusiastic, eager,  or excited], examples=[it was hot inside the hall, a very hot dish cooked with green chili, the idea had been nurtured in his hot imagination], usage=Adjective, parentKey=hot, englishId=133595475665305056)])>

我究竟做错了什么?

标签: javaandroidkotlinjunithamcrest

解决方案


您可能想要覆盖 hashcode 和 equals 方法。由于默认的 equals 方法是按引用而不是按数据进行比较。


推荐阅读