简体   繁体   English

使用 lombok @Getter 和 @Setter 时,单元测试因 this.id 为空而失败,但在实体类上使用 @Data 时有效

[英]Unit test fails with this.id is null when using lombok @Getter and @Setter, but works when using @Data on the Entity class

I am using lombok @Getter and @Setter annotations on my Entity class.我在我的实体类上使用@Getter@Setter注释。

import lombok.Getter;
import lombok.Setter;

@Entity
@Table(name = "Achievements")
@Getter
@Setter
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Achievement extends DomainObject {

  private static final long serialVersionUID = 1L;

  @Column(name = "Name")
  private String name;

  @Column(name = "Details")
  private String details;

  @ManyToOne
  @JoinColumn(name = "AchievementTypeId", nullable = false)
  private AchievementType achievementType;
  
  @OneToMany(cascade = CascadeType.ALL)
  @JoinColumn(name = "QuestId")
  private Quest quest;
}

When I run the following unit test.当我运行以下单元测试时。 it fails:它失败:

@Test
  public void saveAchievementTest() {
    Achievement achievement = new Achievement();
    achievement.setId(UUID.fromString(achievementId));
    AchievementRequestDTO achievementRequestDTO = new AchievementRequestDTO();
    achievementRequestDTO.setAchievementTypeId(UUID.fromString(achievementTypeId));
    achievement.setAchievementType(achievementType);
    when(achievementTypeRepository.getOne(achievementRequestDTO.getAchievementTypeId()))
        .thenReturn(achievementType);
    when(achievementRepository.save(achievement)).thenReturn(achievement);
    Achievement dbAchievement = achievementService.saveAchievement(achievementRequestDTO);
    assertEquals(dbAchievement.getId(), UUID.fromString(achievementId));
  }

The above run fails with a NullPointerException stating that dbAchievement is null .上面的运行失败,出现NullPointerException指出dbAchievement is null However, the same test case works when(in either of the two cases):但是,相同的测试用例适用于(在两种情况中的任何一种情况下):

  1. I use @Data annotation instead of @Getter and @Setter in the entity class.我在实体类中使用@Data批注而不是@Getter@Setter
  2. I change the when statement in the test to when(achievementRepository.save(any())).thenReturn(achievement);我将测试中的when语句改为when(achievementRepository.save(any())).thenReturn(achievement);

I suppose the issue here is the comparison of hashcodes to compare the object passed.我想这里的问题是比较哈希码以比较传递的对象。 I want to stick to using @Getter and @Setter in my entity.我想坚持在我的实体中使用@Getter and @Setter What should I do differently to achieve this?我应该采取什么不同的方式来实现这一目标?

Your test fails because the object you save, doesn't equal the object initialized.您的测试失败,因为您保存的对象不等于初始化的对象。

Lombok's @Data incorporates @ToString @EqualsAndHashCode @Getter @Setter and @RequiredArgsConstructor Lombok 的@Data包含@ToString @EqualsAndHashCode @Getter @Setter@RequiredArgsConstructor

Every Java class has a default no-args constructor if there's none specified (as you have).如果没有指定(如您所见),则每个 Java 类都有一个默认的无参数构造函数。

When you call your test, you're calling for a getId() on a field that's initialized as null当您调用测试时,您是在初始化为null的字段上调用getId()

To solve this, you can seperately add the Lombok @AllArgsConstructor or the @RequiredArgsConstructor要解决这个问题,您可以单独添加 Lombok @AllArgsConstructor@RequiredArgsConstructor

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Lombok 在使用@Data 后不会创建 getter 和 setter 方法 - Lombok does not create getter and setter methods after using @Data Project Lombok 是否与使用 getter 和 setter 的数据封装相矛盾? - Does Project Lombok contradict the data encapsulation of using a getter and setter? 使用 Lombok 的 @Getter 和 @Setter 时,SonarQube 找不到未使用的导入 - SonarQube can't find unused imports when using Lombok's @Getter and @Setter 有没有办法让 lombok 在使用 @Getter @Setter 注释时在 null 的情况下创建对象? - Is there a way to make lombok to create an object in case of null while using @Getter @Setter annotation? 我添加了 Lombok,但是当我运行它时,得到一个 getter / setter 错误 - I added Lombok, but when I run it, get a getter / setter error 使用Ant构建时Lombok无法生成setter和getter - Lombok cannot generate setter and getter while using Ant build 使用getter方法获取值时,应用于setter方法的@Resource注释返回null指针异常 - @Resource annotation applied on setter method is returning the null pointer exception when using getter method to get the value 将 class 与自定义 getter setter 用作 RequestBody 时,WebClient 给出 MediaType 不支持异常 - WebClient giving MediaType not supported Exception when using class with custom getter setter as RequestBody 使用setter getter类通过MVC模型将数据插入数据库 - insert data into database through MVC model using setter getter class 何时使用JavaFX属性setter和getter,而不是直接使用该属性 - When to use JavaFX properties setter and getter, instead of using the property directly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM