У меня есть данные пружины DAO с @EntityGraph
, которые работают нормально (это было проверено вручную).Но я бы хотел написать @DataJpaTest
, чтобы быть уверенным в будущем.И по какой-то причине, только в тесте, весна не присоединилась к столбцу, упомянутому в @EntityGraph
, и они равны нулю.Как исправить тест?
Примечание: тест не пройден на assertEquals
Мой DAO имеет следующий вид:
public interface PersonDAO extends CrudRepository<Person, Long> {
@EntityGraph(value = "includeRemunerationAndCredential", type= EntityGraph.EntityGraphType.FETCH)
@Query("select P from Person P LEFT JOIN P.remunerations r " +
"where P.role = biz.models.Role.ROLE_ADMIN " +
"and P.carWash = ?1 " +
"and P.id = ?2 " +
"and P.enable = true " +
"and r.enable = true " +
"order by P.secondName ")
Person findAdmin(CarWash carWash, Long adminId);
}
тест:
@ExtendWith(SpringExtension.class)
@DataJpaTest
class PersonDAOTest {
@Autowired
private TestEntityManager testEntityManager;
@Autowired
private PersonDAO personDAO;
@Test
void findAdminTest() {
Owner owner = testEntityManager.persist(anyOwner.get());
CarWash carWash = testEntityManager.persist(anyCarWash.apply(owner));
Person person = testEntityManager.persist(anyPerson.apply(carWash));
Remuneration remuneration = testEntityManager.persist(anyRemuneration.apply(person));
Credential credential = testEntityManager.persist(anyCredential.apply(person));
Person admin = personDAO.findAdmin(carWash, person.getId());
assertNotNull(admin);
assertEquals(credential, admin.getCredentials());
}
}
модель:
@Entity
@Table(name = "persons")
@NamedEntityGraph(name = "includeRemunerationAndCredential",attributeNodes = {
@NamedAttributeNode("remunerations"),
@NamedAttributeNode("credentials")
})
@Getter @Setter
@NoArgsConstructor @AllArgsConstructor
@Builder
public class Person implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "first_name", nullable = false, length = 50)
@NotNull
private String firstName;
@Column(name= "last_name", nullable = false, length = 50)
@NotNull
private String lastName;
@Column(name = "father_name", length = 50)
private String fatherName;
@Column(name = "phone_number", length = 20)
private String phoneNumber;
@Column(name = "date_of_birth")
private LocalDateTime dateOfBirth;
@Column(name = "role", nullable = false, length = 50)
@Enumerated(EnumType.STRING)
private Role role;
@Column(name = "date_of_creation", nullable = false)
@NotNull
private LocalDateTime dateOfCreation;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "carwash")
private CarWash carWash;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "person")
private List<Remuneration> remunerations;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "person")
private Set<Credential> credentials;
@Column(name = "enable", nullable = false)
private Boolean enable;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "owner", nullable = true)
private Owner owner;