如果元素的顺序无关紧要,那么您可以使用Set
代替List
. 话虽如此,您可以使用AssertJ 提供的containsExactlyInAnyOrder()方法。此方法需要 var-args 作为参数,因此为了将列表转换为数组,我们可以使用toTypedArray和扩展运算符Eg
import org.junit.Test
import org.assertj.core.api.Assertions.*
data class ServiceFeature(
val flagValue: String?,
val effectiveFlagValue: String?,
val name: String?,
val attributes: List?
)
data class Attribute(val name: String?)
class SimpleTest {
@Test
fun test() {
val list1 = listOf(ServiceFeature("flagA", "effectiveFlagA", "foo", listOf(Attribute("a"), Attribute("b"))))
val list2 = listOf(ServiceFeature("flagA", "effectiveFlagA", "foo", listOf(Attribute("b"), Attribute("a"))))
list1.zip(list2).forEach {
assertThat(it.first.name).isEqualTo(it.second.name)
assertThat(it.first.effectiveFlagValue).isEqualTo(it.second.effectiveFlagValue)
assertThat(it.first.name).isEqualTo(it.second.name)
val toTypedArray = it.second.attributes!!.toTypedArray() // null-check as per your need
assertThat(it.first.attributes).containsExactlyInAnyOrder(*toTypedArray)
}
}
}