gpt4 book ai didi

java - Eclipse JUnit4 和 gradle 测试运行之间 @EqualsAndHashCode 的行为不一致

转载 作者:行者123 更新时间:2023-12-02 01:56:06 24 4
gpt4 key购买 nike

使用 Eclipse JUnit 和 gradle 测试运行单元测试时,我得到不同的结果。有这样的类(class):

@Getter @Setter
@EqualsAndHashCode
public class ObjectWithId {
private Long id;
}

以及类似的测试(将案例压缩到一个测试以节省空间):

@Test
public void testObjectWithId() {

ObjectWithId o1 = new ObjectWithId(), o2 = new ObjectWithId();
o1.setId(1L);
o2.setId(1L);
assertEquals(o1.hashCode(), o2.hashCode());
assertEquals(o1, o2);

o2.setId(2L);
assertNotEquals(o1, o2);
assertNotEquals(o1.hashCode(), o2.hashCode());

}

一切都按预期顺利进行。

然后,有一个类:

@Getter @Setter
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class ObjectWithIdAndDate {

@EqualsAndHashCode.Include
private Long id;

private LocalDateTime created;

}

测试如下:

@Test
public void testObjectWithIdAndDate() {

ObjectWithIdAndDate o1 = new ObjectWithIdAndDate(), o2 = new ObjectWithIdAndDate();
o1.setId(1L);
o2.setId(1L);
assertEquals(o1.hashCode(), o2.hashCode());
assertEquals(o1, o2);

o2.setId(2L);
assertNotEquals(o1, o2);
assertNotEquals(o1.hashCode(), o2.hashCode());

o2.setId(1L);
o2.setCreated(LocalDateTime.now());
// Eclipse JUnit starts failing here because setting the created.
// Gradle test will pass.
assertEquals(o1.hashCode(), o2.hashCode());
assertEquals(o1, o2);

o1.setCreated(LocalDateTime.now());
assertEquals(o1.hashCode(), o2.hashCode());
assertEquals(o1, o2);

}

使用 Eclipse JUnit 运行时失败,但使用 gradle 测试时成功?我从 Eclipse 和命令行运行 gradle 测试,没有区别。所以,似乎 gradle 更好地知道应该如何对待 @EqualsAndHashCode(onlyExplicitlyIncluded = true)...?

我在 build.gradle 中编译了compile 'org.projectlombok:lombok:1.18.2',并在 Eclipse 中安装了相同版本的 lombok.jar。

gradle 项目和 Eclipse 都使用 JUnit 版本 4.12。我在这里缺少什么?

一些进一步的调查:

我用 Maven 构建了其他方面相同的项目。令我惊讶的是,这个项目的 JUnit 测试也通过了。

这听起来像是 Eclipse gradle 项目方面或某些其他 gradle 项目特定设置有问题吗?

最佳答案

您还需要更新 lombok 的 Eclipse 安装。您可以在“帮助”>“关于 Eclipse”屏幕中验证已安装的版本。在白色区域中,底行应告知您已安装的版本。

运行java -jar lombok.jar 来更新您的安装。

关于java - Eclipse JUnit4 和 gradle 测试运行之间 @EqualsAndHashCode 的行为不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52262754/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com