java - 即使具有相同的数据,断言也无法比较两个数据类
问题描述
我有两个数据类 - 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)])>
我究竟做错了什么?
解决方案
您可能想要覆盖 hashcode 和 equals 方法。由于默认的 equals 方法是按引用而不是按数据进行比较。
推荐阅读
- python - 如何在 r 或 python 中找到并突出显示彼此在一定范围内的值簇?
- postgresql - 如何从具有大量记录的表中选择唯一记录
- python - Twython 帮助:有没有办法在 twitter 用户的简历中搜索带有特定关键字的用户?
- android - [GDAL][Android]如何设置GDAL_DATA目录路径?
- python - 字符串转换为数据框
- node.js - Gphoto2 节点 ### io-library 发生错误('Could not claim the USB device')
- spring - 连接多种认证机制 Spring Boot Security
- c++ - 如何摆脱 json 消息中的 POST 详细信息?
- python - GUI中的h2o autoML错误和通过python代码
- javascript - 跟踪来自 Android 原生智能横幅的点击、关闭和下载