用于EntityGraph的Spring Boot DataJpaTest不起作用

时间:2019-02-14 23:30:57

标签: spring-boot testing spring-data-jpa

我有带有@EntityGraph的弹簧数据DAO,它工作正常(已手动测试)。但是我想为此写@DataJpaTest只是为了确保未来。并且由于某种原因,仅在测试中,spring尚未加入@EntityGraph中提到的列,并且它们为null。如何修复测试?

NB: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;

0 个答案:

没有答案