Assert fails in comparing two data classes even though the have same data

Viewed 389

I have two data classes - HindiTranslation and EnglishTranslation. I am using a third data class - HindiAndEnglishTranslation, to sort of map them both in one to many relationship in room database. I am trying to test room Dao by asserting if given and retrieved objects of HindiAndEnglishTranslation are equal or not but the tests are failing even though data in both objects is same.

HindiTranslation:

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

EnglishTranslation:

@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(),
)

HindiAndEnglishTranslation:

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

Code in Dao test:

    @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))))

    }

The error I am getting:

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)])>

What am I doing wrong?

2 Answers

you may want to override the hashcode and equals method. Since the default equals method compares by reference and not by data.

You should use assertContentEquals(expectedArray, actualArray) instead of assertThat(expectedArray, actualArray)

Related