assertEquals未正确计算参数化的测试junit

dm7nw8vv  于 2023-10-20  发布在  其他
关注(0)|答案(1)|浏览(108)

我正在创建一个测试来测试我的类Map器是否正确工作。Map器是正确的,但不知何故,assertEquals没有正确识别出我的实际和预期是相等的,它说它指向不同的对象,所以它不相等。

@RunWith(Parameterized::class)
class PaginationToPresentationMapperTest(
    private val given: PaginationDomainModel,
    private val expected: PaginationPresentationModel
) {
    companion object {
        @JvmStatic
        @Parameters(name = "Given {0} then {1}")
        fun data(): Collection<Array<*>> = listOf(
            testCase(
                nextCursor = null,
                prevCursor = null
            ),
            testCase(
                nextCursor = "next_id",
                prevCursor = null
            ),
            testCase(
                nextCursor = null,
                prevCursor = "prev_id"
            )
        )

        private fun testCase(
            nextCursor: String?,
            prevCursor: String?
        ) = arrayOf(
            PaginationDomainModel(
                nextCursor = nextCursor,
                prevCursor = prevCursor
            ),
            PaginationPresentationModel(
                nextCursor = nextCursor,
                prevCursor = prevCursor
            )
        )
    }

    private lateinit var classUnderTest: PaginationToPresentationMapper

    @Before
    fun setUp() {
        classUnderTest = PaginationToPresentationMapper()
    }

    @Test
    fun `When toPresentation`() {
        // given

        // when
        val actual = classUnderTest.toPresentation(given)

        // then
        assertEquals(expected, actual)
    }
}

不合格:

expected:<link.limecode.newsfeed.presentation.model.pagination.PaginationPresentationModel@7bd7d6d6> but was:<link.limecode.newsfeed.presentation.model.pagination.PaginationPresentationModel@50029372>
Expected :link.limecode.newsfeed.presentation.model.pagination.PaginationPresentationModel@7bd7d6d6
Actual   :link.limecode.newsfeed.presentation.model.pagination.PaginationPresentationModel@50029372
tcomlyy6

tcomlyy61#

您需要覆盖PaginationPresentationModelequals方法。默认情况下,equals方法只比较示例相等性,因此具有相同字段值的两个不同对象将不相等。
如果你使用data class,Kotlin会自动为你生成一个基于值的equals。

相关问题