我正在将一个Java项目转换为Kotlin。我已经将一个User对象转换为Kotlin,当我在JUnit中运行现有的JUnit测试时,我在Kotlin User对象的两个实例之间出现了一个错误。
User.kt:
data class User (
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
var id: Long? = null,
...
)TestUtil.java
import static org.assertj.core.api.Assertions.assertThat;
public class TestUtil {
public static void equalsVerifier(Class clazz) throws Exception {
Object domainObject1 = clazz.getConstructor().newInstance();
// Test with an instance of the same class
Object domainObject2 = clazz.getConstructor().newInstance();
assertThat(domainObject1).isNotEqualTo(domainObject2);
}
}assertThat(domainObject1).isNotEqualTo(domainObject2)测试失败了,因为我相信比较在Kotlin类上做得不正确。如果我通过调试器运行它,我可以看到domainObject1和domainObject2是不同的实例。
能让这个测试用例通过吗?相同的测试用例用于其他Java类,因此它必须同时适用于Java和Kotlin类。
发布于 2017-08-15 06:15:47
isNotEqualTo调用equals。Kotlin类为data class实现了正确的data class方法。所以domainObject1.equals(domainObject2)是真的。这种行为是正确的。
只需查看AssertJ文档:
isNotSameAs(Object other):
Verifies that the actual value is not the same as the given one,
ie using == comparison.我觉得你应该试试:
assertThat(domainObject1).isNotSameAs(domainObject2);发布于 2017-08-15 06:38:46
https://stackoverflow.com/questions/45687125
复制相似问题