jeudi 14 février 2019

Spring Boot DataJpaTest for EntityGraph doesn't work

I have the spring data DAO with @EntityGraph that works fine (it was tested manually). But I would like to write @DataJpaTest for it just to be sure about the future. And for some reason, only in test, spring hasn't joined the column that was mentioned in @EntityGraph and they are null. How to fix test?

NB: test fails on the assertEquals

My DAO has the following view:

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);
}

the test:

@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());
}

}

the model:

@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;

Aucun commentaire:

Enregistrer un commentaire