如果使用Lombok的@Data注解而不是单独的@Getter和@Setter注解,我的实体之间会有一个关系抛出一个堆栈溢出错误。现在这个问题已经解决了,但是我想在我的存储库测试中为它编写一个单元测试。但是,我不确定如何实现它,也没有找到它的示例。
下面是我的实体类:
@Entity
@Table(name = "users")
@Builder
//@Getter
//@Setter
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private UUID id;
@Column(name = "name")
private String name;
@ManyToMany
@JoinTable(
name = "users_hobbies",
joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "hobby_and_interest_id", referencedColumnName = "id"))
private Set<HobbyAndInterestEntity> hobbyAndInterestEntities;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
@JoinColumn(name = "hometown_id", referencedColumnName = "id")
private HometownEntity hometownEntity;
@Entity
@Table(name = "hometown")
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
public class HometownEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private UUID id;
@Column(name = "city")
private String city;
@Column(name = "country")
private String country;
@OneToMany(mappedBy = "hometownEntity", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = false)
private Set<UserEntity> userEntitySet;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class HobbyAndInterestEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private UUID id;
@Column(name = "title")
private String title;
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "hobbyAndInterestEntities")
private Set<UserEntity> userEntities;
下面是我对一个没有异常的用例的测试,我打算修改它来测试异常场景:
@Test
void testGetUser() {
UserEntity userEntity = saveUserEntity();
assertTrue(userRepository.findAll().size() > 0);
userEntity = userRepository.findById(userEntity.getId()).orElse(null);
assertNotNull(userEntity);
UserEntity finalUserEntity = userEntity;
assertAll(
() -> assertEquals("anyName", finalUserEntity.getName()),
() -> assertEquals("anyCountry", finalUserEntity.getHometownEntity().getCountry()),
() -> assertTrue(finalUserEntity.getHobbyAndInterestEntities().size() > 0));
finalUserEntity.getHobbyAndInterestEntities().forEach(h -> assertEquals("anyInterest", h.getTitle()));
}
@NotNull
private UserEntity saveUserEntity() {
HometownEntity hometownEntity = HometownEntity.builder().city("anyCity").country("anyCountry").build();
hometownEntity = hometownRepository.save(hometownEntity);
HobbyAndInterestEntity hobbyAndInterestEntity = HobbyAndInterestEntity.builder()
.title("anyInterest")
.build();
hobbyAndInterestEntity = hobbyAndInterestRepository.save(hobbyAndInterestEntity);
Set<HobbyAndInterestEntity> hobbyAndInterestEntities = new HashSet<>();
hobbyAndInterestEntities.add(hobbyAndInterestEntity);
UserEntity userEntity = UserEntity.builder()
.name("anyName")
.hometownEntity(hometownEntity)
.hobbyAndInterestEntities(hobbyAndInterestEntities)
.build();
return userRepository.save(userEntity);
}
总之,我知道当我有@Data注解时,应用程序会抛出堆栈溢出,所以我想编写一个测试,当我修改实体类以使用@Getter和@Setter时,该测试会失败并再次通过,但不确定这里需要什么,希望能得到一些指导。
非常感谢。
1条答案
按热度按时间k4emjkb11#
请检查
@Data
注解here。@Data
是所有字段上@ToString
、@EqualsAndHashCode
、@Getter
、所有非最终字段上@Setter和@RequiredArgsConstructor
的快捷方式!当您调用toString
或equals
或hashCode
方法时,关系实体将在数据库中查询。您可以尝试查看生成的源代码,在那些方法中使用了关系实体。我认为它可能会抛出堆栈溢出错误。